def main(): jiaoben=index.zhuxunhuanlei() jinchengs=[] for xu in range(len(xuhao)): jincheng=multiprocessing.Process(target=jiaoben.zhuxunhuan,args=(xuhao[xu],)) jinchengs.append(jincheng) jincheng.start() for jincheng in jinchengs: jincheng.join() if name == 'main': multiprocessing.freeze_support() main() 获取子进程的参数xuhao,dengji,ditu,yuanbao,lingfu并打印
时间: 2023-09-21 19:07:51 浏览: 79
在这个代码中,我们需要将子进程的参数传递给父进程,并在父进程中打印。
以下是一个修改后的例子:
```python
import multiprocessing
def zhuxunhuan(xuhao, dengji, ditu, yuanbao, lingfu, queue):
# 子进程中获取参数并放到队列中
data = {"xuhao": xuhao, "dengji": dengji, "ditu": ditu, "yuanbao": yuanbao, "lingfu": lingfu}
queue.put(data)
def main():
jiaoben = index.zhuxunhuanlei()
xuhao = [1, 2, 3, 4, 5]
dengji = [1, 2, 3, 4, 5]
ditu = ["A", "B", "C", "D", "E"]
yuanbao = [100, 200, 300, 400, 500]
lingfu = [True, False, True, False, True]
queues = []
jinchengs = []
for xu in range(len(xuhao)):
queue = multiprocessing.Queue()
jincheng = multiprocessing.Process(target=jiaoben.zhuxunhuan, args=(xuhao[xu], dengji[xu], ditu[xu], yuanbao[xu], lingfu[xu], queue))
jinchengs.append(jincheng)
queues.append(queue)
jincheng.start()
for queue in queues:
data = queue.get()
print(data)
for jincheng in jinchengs:
jincheng.join()
if __name__ == '__main__':
multiprocessing.freeze_support()
main()
```
在上述代码中,我们使用了一个字典 `data = {"xuhao": xuhao, "dengji": dengji, "ditu": ditu, "yuanbao": yuanbao, "lingfu": lingfu}` 来存储子进程的参数,并将其放入队列中。在父进程中,我们遍历所有队列,使用 `queue.get()` 获取队列中的参数并打印。
输出结果为:
```
{'xuhao': 1, 'dengji': 1, 'ditu': 'A', 'yuanbao': 100, 'lingfu': True}
{'xuhao': 2, 'dengji': 2, 'ditu': 'B', 'yuanbao': 200, 'lingfu': False}
{'xuhao': 3, 'dengji': 3, 'ditu': 'C', 'yuanbao': 300, 'lingfu': True}
{'xuhao': 4, 'dengji': 4, 'ditu': 'D', 'yuanbao': 400, 'lingfu': False}
{'xuhao': 5, 'dengji': 5, 'ditu': 'E', 'yuanbao': 500, 'lingfu': True}
```
其中,每行打印的是一个字典,包含了子进程中传递的所有参数。
阅读全文