简体   繁体   中英

Main thread stuck in Python Multithreading

I have 2 simple functions:

def run(self):
    # Make 20 instances of clients and start those
    for i in xrange(0,20):
        t = threading.Thread(target = self.run_clients_in_seperate_threads())
        t.start()

and

def run_clients_in_seperate_threads(self):
    print 'inside run_clients_in_seperate_threads'
    client_id = self.generate_client_id()
    cl = Client(client_id)
    cl.start()

Here, the last line: cl.start() is an infinite loop.

I was thinking that the main thread will become free after starting the child threads, and hence will spawn 20 threads in total. But it seems that the main thread waits after starting the 1st thread.

Can someone please explain what I'm doing wrong ?

Use target = self.run_clients_in_seperate_threads and pass self to the args parameter . The way you way you do it you invoke the method in the main thread and end up with the infinite loop there : self.run_clients_in_seperate_threads != self.run_clients_in_seperate_threads()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM