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?

Python_Dude
  • 507
  • 6
  • 12
  • 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. – Oliver Friedrich Jan 15 '14 at 11:37

1 Answers1

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 ")
Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • 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 – Python_Dude Jan 15 '14 at 10:25
  • a loop like this should not cause a problem. You are getting that error because [the server doesn't want to talk to you](http://stackoverflow.com/questions/1434451/what-does-connection-reset-by-peer-mean) for whatever reason. You probably have some problem in your netcode or network configuration. – Serdalis Jan 15 '14 at 10:28