按顺序执行python线程
我有一个列表和n个线程。我想使用一些IPC机制以一定的顺序附加到这个列表中,顺序是第一个线程先写入列表,然后第二个线程等等。按顺序执行python线程,python,multithreading,Python,Multithreading,我有一个列表和n个线程。我想使用一些IPC机制以一定的顺序附加到这个列表中,顺序是第一个线程先写入列表,然后第二个线程等等。 我唯一想到的是在前一个线程完成它的任务后使用n个锁并解锁另一个线程的锁,但我认为这不是正确的解决方案。感谢您的帮助。您可以使用队列模块执行以下操作: from Queue import * from threading import Thread, Lock # this function will process the items in the queue, in
我唯一想到的是在前一个线程完成它的任务后使用n个锁并解锁另一个线程的锁,但我认为这不是正确的解决方案。感谢您的帮助。您可以使用队列模块执行以下操作:
from Queue import *
from threading import Thread, Lock
# this function will process the items in the queue, in serial
def processor():
if queue.empty() == True:
print "the Queue is empty!"
sys.exit(1)
try:
job = queue.get()
print "I'm operating on job item: %s"%(job)
queue.task_done()
except:
print "Failed to operate on job"
# set variables
queue = Queue()
threads = 4
''' a list of job items. you would want this to be more advanced,
like reading from a file or database'''
jobs = [ "job1", "job2", "job3" ]
# iterate over jobs and put each into the queue in sequence
for job in jobs:
print "inserting job into the queue: %s" % (job)
queue.put(job)
# start some threads, each one will process one job from the queue
for i in range(threads):
th = Thread(target=processor)
th.setDaemon(True)
th.start()
# wait until all jobs are processed before quitting
queue.join()
关于队列模块的更多信息,在本页末尾有一个god示例:
@编辑
您可以使用FIFOfirst in、first out或LIFOlast in、first out队列
先进先出示例:
后进先出:
posible解决方案是将iterable放在字典中,整数作为字符串键,然后将结果放回该键。 将命令列在结果上 进口期货 导入集合
def x(y):
import random
if y == 100:
import time
time.sleep(2)
return y * 1000000000
def run(ordered, func, list_):
print(f'ordred: {ordered}')
if ordered:
list_ = collections.OrderedDict({i: list_[i] for i in range(len(list_))})
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as thd:
res = {thd.submit(func, v): k for k, v in list_.items()}
for fut in concurrent.futures.as_completed(res):
list_[res[fut]] = fut.result()
return [value for value in list_.values()]
else:
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as thd:
res = {thd.submit(func, v): v for v in list_}
return [
fut.result()
for fut in concurrent.futures.as_completed(res)
]
v = run(True, x, [60, 70, 80, 90, 91, 100,101,102])
print(v)
print(v == [60000000000, 70000000000, 80000000000, 90000000000, 91000000000, 100000000000, 101000000000, 102000000000])
v = run(False, x, [60, 70, 80, 90, 91, 100,101,102])
print(v)
print(v == [60000000000, 70000000000, 80000000000, 90000000000, 91000000000, 100000000000, 101000000000, 102000000000])
结果:
ordred: True
[60000000000, 70000000000, 80000000000, 90000000000, 91000000000, 100000000000, 101000000000, 102000000000]
True
ordred: False
[90000000000, 60000000000, 70000000000, 80000000000, 91000000000, 101000000000, 102000000000, 100000000000]
False
在何种意义上,你认为这个解决方案是不正确的吗?请尝试一些代码片段,看看它不是一个正确的解决方案。你应该先尝试…你应该只相信你所看到的。。。我知道一开始很难对解决方案不确定,然后仍然尝试……但结果很好:你在想什么?
def x(y):
import random
if y == 100:
import time
time.sleep(2)
return y * 1000000000
def run(ordered, func, list_):
print(f'ordred: {ordered}')
if ordered:
list_ = collections.OrderedDict({i: list_[i] for i in range(len(list_))})
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as thd:
res = {thd.submit(func, v): k for k, v in list_.items()}
for fut in concurrent.futures.as_completed(res):
list_[res[fut]] = fut.result()
return [value for value in list_.values()]
else:
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as thd:
res = {thd.submit(func, v): v for v in list_}
return [
fut.result()
for fut in concurrent.futures.as_completed(res)
]
v = run(True, x, [60, 70, 80, 90, 91, 100,101,102])
print(v)
print(v == [60000000000, 70000000000, 80000000000, 90000000000, 91000000000, 100000000000, 101000000000, 102000000000])
v = run(False, x, [60, 70, 80, 90, 91, 100,101,102])
print(v)
print(v == [60000000000, 70000000000, 80000000000, 90000000000, 91000000000, 100000000000, 101000000000, 102000000000])
ordred: True
[60000000000, 70000000000, 80000000000, 90000000000, 91000000000, 100000000000, 101000000000, 102000000000]
True
ordred: False
[90000000000, 60000000000, 70000000000, 80000000000, 91000000000, 101000000000, 102000000000, 100000000000]
False