![](/img/trans.png)
[英]How to pass an object to a process created using Multiprocess.Process
[英]How to append items to a list called over multiprocess.Process for use outside?
我想用在get_list函数中运行的命令的输出创建一个新列表(将返回JSON数据)。 它既可以放入一个索引中,也可以放入子列表之类的索引中-我只需要能够访问该函数之外的列表中的信息。 不知道我希望如何将该信息返回或存储在函数外部。
def get_list(accountID):
# call another script here that gets JSON data,
# appends to list, and can be accessed globally
# newList[index][sub-index] or newList[index]
data = subprocess.check_output(['sudo', '/path/to/script.pl', serviceNumber])
data = json.loads(data)
# breaks data into separate lists consisting of no more than 3 objects
sublists = [data[i:i+3] for i in xrange(0, len(data), 3)]
for sublist in sublists:
for obj in sublist):
p = Process(target=get_list, args=(obj['account_id'],))
p.start()
任何帮助表示赞赏。
听起来像是多处理的Pool().map
:
from multiprocessing import Pool
if __name__=="__main__":
p = Pool(processes=3)
newList = p.map(get_list, [obj['account_id'] for obj in data])
让get_list
返回列表:
def get_list(accountID):
# call another script here that gets JSON data
return result_list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.