为了账号安全,请及时绑定邮箱和手机立即绑定

如下,但执行的时候并没有返回任何参数

如下,但执行的时候并没有返回任何参数

白衣非少年 2022-08-04 23:19:14
p=Pool(20)with open(temp_path,'r') as f:for line in f.readlines():p.apply_async(execute_monitor_sql,args=(type,)) --其中execute_monitor_sql函数会返回一个执行结果。我需要将这些结果汇总到一个参数p.close()p.join()
查看完整描述

2 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

共享变量的方法。

没有办法直接实现你的需求,但是,你可以用共享变量的方法实现,比如:

def worker(procnum, return_dict):

'''worker function'''

print str(procnum) + ' represent!'

return_dict[procnum] = procnumif __name__ == '__main__':

manager = Manager()

return_dict = manager.dict()

jobs = []    for i in range(5):

p = multiprocessing.Process(target=worker, args=(i,return_dict))
jobs.append(p)

p.start()    for proc in jobs:

proc.join()    print return_dict.values()


查看完整回答
反对 回复 2022-08-08
?
qq_遁去的一_1

TA贡献1725条经验 获得超8个赞

交换数据一般用queue吧。

17.2.1.3. Exchanging objects between processes

multiprocessing supports two types of communication
channel between processes:

Queues

The Queue
class is a near clone of queue.Queue. For example:

from multiprocessing import Process, Queue

def f(q):
q.put([42, None, 'hello'])

if __name__ == '__main__':
q = Queue()
p = Process(target=f, args=(q,))
p.start()
print(q.get()) # prints "[42, None, 'hello']"
p.join()

Queues are thread and process safe.

Pipes

The Pipe() function returns a pair of connection objects
connected by a pipe which by default is duplex (two-way). For example:

from multiprocessing import Process, Pipe

def f(conn):
conn.send([42, None, 'hello'])
conn.close()

if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(child_conn,))
p.start()
print(parent_conn.recv()) # prints "[42, None, 'hello']"
p.join()

The two connection objects returned by Pipe()
represent the two ends of the pipe. Each connection object has send()
and recv()
methods (among others). Note that data in a pipe may become corrupted if two
processes (or threads) try to read from or write to the same end of the
pipe at the same time. Of course there is no risk of corruption from processes
using different ends of the pipe at the same time.


查看完整回答
反对 回复 2022-08-08
  • 2 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号