1

I want to create 3 threads in python ,I am using python class thread library, Is this the below is the right way to create threads in while loop? it may create problems?

while (count <= 3):
     try:
        thread = CreateThread(count, args)
        thread.start()
     except:
        logger.error("Error: unable to start thread ")

Any other right ways?

1
  • Seems like your problem is not really defined here. Please overwatch your question and tell us the problem. According to your comment on @Serdalis answer, you have a problem connecting to your router multiple times, not a problem to start threads. Remember that most plastic-routers have a connection limit. Commented Jan 15, 2014 at 11:37

1 Answer 1

3

Although We can't see your actual Thread class i'll assume its correct, there is something in this code that can be improved.

You will want to keep the references to each thread so you can stop / join / wait for them at a later time.

So your code should look more like:

thread = []
for i in range(3):
    try:
        new_thread = CoreRouterThread(count, args)
        new_thread.start()
        # we append the thread here so we don't get any failed threads in the list.
        thread.append(new_thread)
     except:
        logger.error("Error: unable to start thread ")
Sign up to request clarification or add additional context in comments.

2 Comments

thanks serdalis...each threads used to connect to router and fetch data...due to this loop causes any problem?..I am getting connection reset by peer error
a loop like this should not cause a problem. You are getting that error because the server doesn't want to talk to you for whatever reason. You probably have some problem in your netcode or network configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.