使用队列在多处理内部进行线程化

时间:2018-07-02 06:24:21

标签: multithreading python-2.7 queue multiprocessing

#!/usr/bin/env python2.7
import multiprocessing
from threading import Thread
from Queue import Queue

def do_stuff(q):
    print ("its do_stuff fn - {}".format(q.get()))

def start_my_work(i,q):
    print ("Start work for {}".format(q))

    t_source = ['My','Name','Is','John','I','Work','In','IT']

    qu = Queue()

    num_threads = 2

    for i in range(num_threads):
        worker = Thread(target=do_stuff, args=(qu,))
        worker.setDaemon(True)
        worker.start()

    for item in t_source:
        qu.put(item)

    qu.join()


q = multiprocessing.Queue()
jobs = []

p_source = ['hi','there','how','are','you','doing']

for item in p_source:
  q.put(item)

for i in range(3): # 3 multiprocess
    p = multiprocessing.Process(target=start_my_work, args=(i,q))
    jobs.append(p)
    p.start()

for p in jobs: #join on all processes ...
    p.join()

print "List processing complete."

大家好,

我正在尝试在多处理的每个过程中实现线程化。我有2个队列,一个用于多进程(进程),另一个在每个进程内。当我执行它低于输出以下挂起。    我的目标是遍历p_source队列,并且每个进程都拾取所有t_source队列(并行),因此我在进程内部使用了线程。

请纠正我在这里错过的内容。谢谢!

Start work for <multiprocessing.queues.Queue object at 0x7f9fdfc1bc90>
its do_stuff fn - My
 its do_stuff fn - Name
Start work for <multiprocessing.queues.Queue object at 0x7f9fdfc1bc90>
its do_stuff fn - My
Start work for <multiprocessing.queues.Queue object at 0x7f9fdfc1bc90>
 its do_stuff fn - Name
its do_stuff fn - My
 its do_stuff fn - Name

我添加了一个更新的代码版本,获得了预期的输出,但是没有队列逻辑。请帮助查看&是否有其他替代方法

#!/usr/bin/env python2.7
import multiprocessing
from threading import Thread

def do_stuff(i,s):
    print ("work from thread {} - list is {}".format(i,s))

def start_my_work(i,q):
    print ("Start work {} for {}".format(i,q.get()))
    t_source = ['My','Name','Is','John','I','Work','In','IT']
    num_threads = 3
    for s in t_source:
        for i in range(num_threads):
            worker = Thread(target=do_stuff, args=(i,s))
            worker.setDaemon(True)
            worker.start()

jobs = []

p_source = ['hi','there','how','are','you','doing','Jay']

res = [p_source[x:x+2] for x in xrange(0, len(p_source), 2)]

for i,l in enumerate(res):
    for n in range(len(res[i])):
        val = res[i][n]
        p = multiprocessing.Process(target=start_my_work, args=(n,val))
        jobs.append(p)
        p.start()

    for p in jobs: #join on all processes ...
        p.join()

    print ("Completed for batch value --> {}".format(val)

print "List processing complete."

问候 坎南

0 个答案:

没有答案