如何使用Python工作池处理并发变量写入
有没有一种特殊的方法可以使用Python的workerpool来写入特定作业之外的变量。
举例来说,您有一个名为 DownloadJob 的作业,它会下载一些文件并递增计数器。处理此计数器变量递增的最佳方法是什么?我是否需要在写入计数器之前以某种方式锁定计数器,或者这是Python自动执行的操作?
谢谢!
Is there a special way to use Python's workerpool to write to a variable that is outside a particular job.
Let's say, for example, you have a job called DownloadJob which downloads some file and increments a counter. What is the best way to handle the incrementing of this counter variable? Do I need to somehow lock the counter before writing to it or is this something that Python does automatically?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
threading.Lock
进行互斥。文档对它们非常清楚: http://docs.python.org/library /threading.html#lock-objects干杯!
You can use
threading.Lock
for mutual exclusion. The documentation is pretty clear about them: http://docs.python.org/library/threading.html#lock-objectsCheers!