Python多线程中主线程被卡住
我有两个简单的函数:
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()
还有一个:
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()
在这里,最后一行:cl.start()
是一个无限循环。
我原本以为,主线程在启动子线程后会变得空闲,这样总共会启动20个线程。
但似乎主线程在启动第一个线程后就一直在等待。
有人能帮我解释一下我哪里做错了吗?
1 个回答
1
使用 target = self.run_clients_in_seperate_threads
,并把 self
传给 args
参数。你现在的做法是在主线程中调用这个方法,这样就会导致无限循环: self.run_clients_in_seperate_threads != self.run_clients_in_seperate_threads()