linux - 关于python中的Queue与daemon进程?
PHPz
PHPz 2017-04-18 09:47:47
0
1
461

https://docs.python.org/2.7/library/queue.html

def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

q = Queue()
for i in range(num_worker_threads):
     t = Thread(target=worker)
     t.daemon = True
     t.start()

for item in source():
    q.put(item)

q.join()       # block until all tasks are done

A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left.

上面这个例子中,同时设置q.join()和t.daemon = True是不是自相矛盾?设置q.join()的话也就是说要等Queue中所有thread发送task_done信号,也就是说要让Queue里面的所有thread全部执行完才继续执行下一步。既然这样的话还设置t.daemon = True干什么呢?t.daemon=True的目的不是让守护进程在后台执行吗?

Setting daemon to True will let the main thread exit even though thworkers are blocking

q.join() Causes the main thread to wait for the queue to finish processing all the tasks

PHPz
PHPz

学习是最好的投资!

reply all(1)
PHPzhong

Isn’t the purpose of t.daemon=True to let the daemon process execute in the background?

No.

First of all, it is not a process.

Secondly, "background" is a term for job control (a function that is basically implemented by the command line shell; other programs basically ignore it). Even if you are using multiple processes here, it does not apply to your scenario.

Finally, the settings here are a bit redundant. But it's just "a little bit".

Multi-threading is not easy to get right. Signals are another thing. Put the two things together, your Python process is disobedient~ (In Python, signals will only be submitted to the main thread for processing. The main thread decides to exit after receiving the signal, but if the non-daemon process does not actively exit, the process will hang there. So when I write multi-threaded crawlers, I unify daemon = True, so that it can stop quickly when Ctrl-C)

.

By the way, about a month or two ago, the python-cn list discussed the topic of Python daemon threads. If you want to know more about it, you can go and take a look.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template