python - 多处理工作者返回错误响应python
问题描述
请问你能帮帮我吗。
我写了一个与以下非常相似的脚本。函数返回的get_examples(word)
示例word
,我不知道多处理工作者出了什么问题。当words list
很长时,get_examples(word)
函数不会返回 的示例word
,而是返回 的示例get_examples(other_word)
。
该错误仅在words list
长时发生。
from multiprocessing.dummy import Pool as ThreadPool
def get_examples(word):
#do someting ...
eg = f'eg: {word}'
return eg
def worker(word):
data={}
data['word'] = word
data['examples'] = get_examples(word) #error???
with open(f'{word}.json', 'w') as outfile:
json.dump(data, outfile, indent=4)
words=['hi','go']
pool = ThreadPool(100)
pool.starmap(worker, zip(words))
pool.close()
pool.join()
我得到这样的json文件:
hi.json --> {'word': 'hi','examples': 'eg: go'}
go.json --> {'word': 'go','examples': 'eg: hi'}
这是我的真实代码
解决方案
推荐阅读
- android - 保持服务和应用程序代码分开
- powershell - 我可以通过 mstsc 进行 RDP 并在该远程桌面上运行脚本吗?
- android - Android ANR,服务正在锁定主 GUI 活动
- laravel - Laravel 实时使用 socket.io
- node.js - 检查数组,如果每 2 个元素缺少一个值
- bezier - 使用 cytoscape 生成贝塞尔曲线 - 我应该使用哪些参数?
- ruby-on-rails - 使用一台控制器处理数据,无需模型和数据库
- sql - 使用 if else 的 SQL 查询
- java - 对象和基元之间的性能和设计差异
- javascript - 如何在javascript中添加触摸事件(屏幕上的右、左和下)?