gpt4 book ai didi

python - 终止线程: Python

转载 作者:太空宇宙 更新时间:2023-11-03 19:07:32 33 4
gpt4 key购买 nike

在下面的示例中,如果多次执行该程序,它每次都会生成一个具有新 ID 的新线程。1. 如何在任务完成时终止所有线程?2. 如何为线程分配名称/ID?

import threading, Queue 

THREAD_LIMIT = 3
jobs = Queue.Queue(5) # This sets up the queue object to use 5 slots
singlelock = threading.Lock() # This is a lock so threads don't print trough each other

# list
inputlist_Values = [ (5,5),(10,4),(78,5),(87,2),(65,4),(10,10),(65,2),(88,95),(44,55),(33,3) ]

def DoWork(inputlist):
print "Inputlist received..."
print inputlist

# Spawn the threads
print "Spawning the {0} threads.".format(THREAD_LIMIT)
for x in xrange(THREAD_LIMIT):
print "Thread {0} started.".format(x)
# This is the thread class that we instantiate.
worker().start()

# Put stuff in queue
print "Putting stuff in queue"
for i in inputlist:
# Block if queue is full, and wait 5 seconds. After 5s raise Queue Full error.
try:
jobs.put(i, block=True, timeout=5)
except:
singlelock.acquire()
print "The queue is full !"
singlelock.release()

# Wait for the threads to finish
singlelock.acquire() # Acquire the lock so we can print
print "Waiting for threads to finish."
singlelock.release() # Release the lock
jobs.join() # This command waits for all threads to finish.

class worker(threading.Thread):
def run(self):
# run forever
while 1:
# Try and get a job out of the queue
try:
job = jobs.get(True,1)
singlelock.acquire() # Acquire the lock
print self
print "Multiplication of {0} with {1} gives {2}".format(job[0],job[1],(job[0]*job[1]))
singlelock.release() # Release the lock
# Let the queue know the job is finished.
jobs.task_done()
except:
break # No more jobs in the queue


def main():
DoWork(inputlist_Values)

最佳答案

How do I terminate all the threads on task completion?

您可以将 THREAD_LIMIT 哨兵值(例如 None)放在队列末尾,并在以下情况下退出线程的 run() 方法:线程看到它。

在主线程退出时,所有非守护线程都会加入,因此如果任何线程处于事件状态,程序将继续运行。守护线程在程序退出时终止。

How can I assign name/ID to the threads ?

您可以通过将名称传递给构造函数或直接更改 .name 来分配名称。

线程标识符.ident是一个只读属性,在事件线程中是唯一的。如果一个线程退出而另一个线程启动,它可能会被重用。

<小时/>

您可以使用 multiprocessing.dummy.Pool 重写代码,它提供与 multiprocessing.Pool 相同的接口(interface)但使用线程而不是进程:

#!/usr/bin/env python
import logging
from multiprocessing.dummy import Pool

debug = logging.getLogger(__name__).debug

def work(x_y):
try:
x, y = x_y # do some work here
debug('got %r', x_y)
return x / y, None
except Exception as e:
logging.getLogger(__name__).exception('work%r failed', x_y)
return None, e

def main():
logging.basicConfig(level=logging.DEBUG,
format="%(levelname)s:%(threadName)s:%(asctime)s %(message)s")

inputlist = [ (5,5),(10,4),(78,5),(87,2),(65,4),(10,10), (1,0), (0,1) ]
pool = Pool(3)
s = 0.
for result, error in pool.imap_unordered(work, inputlist):
if error is None:
s += result
print("sum=%s" % (s,))
pool.close()
pool.join()

if __name__ == "__main__":
main()

Output

DEBUG:Thread-1:2013-01-14 15:37:37,253 got (5, 5)
DEBUG:Thread-1:2013-01-14 15:37:37,253 got (87, 2)
DEBUG:Thread-1:2013-01-14 15:37:37,253 got (65, 4)
DEBUG:Thread-1:2013-01-14 15:37:37,254 got (10, 10)
DEBUG:Thread-1:2013-01-14 15:37:37,254 got (1, 0)
ERROR:Thread-1:2013-01-14 15:37:37,254 work(1, 0) failed
Traceback (most recent call last):
File "prog.py", line 11, in work
return x / y, None
ZeroDivisionError: integer division or modulo by zero
DEBUG:Thread-1:2013-01-14 15:37:37,254 got (0, 1)
DEBUG:Thread-3:2013-01-14 15:37:37,253 got (10, 4)
DEBUG:Thread-2:2013-01-14 15:37:37,253 got (78, 5)
sum=78.0

关于python - 终止线程: Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14268431/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com