简体   繁体   English

使用python多线程处理线程还不活跃吗?

[英]Threads are not alive using python multithreading?

I'm using the multithreading module of python but the code doesn't work sometimes. 我正在使用python的多线程模块,但是代码有时无法正常工作。 I print the count of active threads and found that sometimes the started threads are not active. 我打印了活动线程的计数,发现有时启动的线程不是活动的。 Here is the code: 这是代码:

def do_stuff(q,obj):
    while not q.empty():
        item=q.get()
        print item
        q.task_done()

for i in range(num_thread):
    worker=threading.Thread(target=do_stuff,args=(q,Files,))
    worker.setDaemon(True)
    worker.start()
    print worker.is_alive()
print threading.active_count()
print threading.enumerate()

I got False for the is_alive() function and there's only one thread (the main thread) in the active thread list. 对于is_alive()函数,我得到了False,并且活动线程列表中只有一个线程(主线程)。

What am I doing wrong here? 我在这里做错了什么?

Thanks a lot! 非常感谢!

Your threads did not have time to start yet. 您的线程还没有时间开始。

As per documentation 根据文档

is_alive() isAlive() is_alive() isAlive()

Return whether the thread is alive. 返回线程是否处于活动状态。

This method returns True just before the run() method starts until just after the run() method terminates. 该方法run()方法开始之前直到run()方法终止之后返回True The module function enumerate() returns a list of all alive threads. 模块函数enumerate()返回所有活动线程的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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