在订单中执行python线程

时间:2014-04-19 17:00:01

标签: python multithreading

我有listn个帖子。我想使用一些IPC机制按特定顺序附加到此列表,顺序是第一个线程首先写入list然后写入第二个线程等。 我想出的唯一事情就是在完成任务之后使用n个锁并解锁另一个线程的锁,但我不相信这是正确的解决方案。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

您可以使用“队列”模块执行此操作:

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() 

有关队列模块的更多信息,页面末尾有一个上帝示例: https://docs.python.org/2/library/queue.html

<强> @EDIT 您可以使用Queue FIFO (先进先出)或 LIFO (后进先出)。

FIFO 示例:

import Queue

q = Queue.Queue()

for i in range(5):
    q.put(i)

while not q.empty():
    print q.get()

>>> 0
>>> 1
>>> 2
>>> 3
>>> 4

<强> LIFO

import Queue

q = Queue.LifoQueue()

for i in range(5):
    q.put(i)

while not q.empty():
    print q.get()

>>> 4
>>> 3
>>> 2
>>> 1
>>> 0

答案 1 :(得分:0)

Posibble解决方案是将带有整数作为字符串键的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