pycharm中多进程踩坑记录--Python

pycharm多进程踩坑–Python

所遇到的情况
如下:

在进程中创建类实例,回调函数去获取实例时,报如下错误,注意在cmd和linux下面运行是没问题的。

执行的代码
如下:

# --coding: utf-8 --
# @User: JasonHU
# @Time: 2020/11/25 21:11
# @IDE: Pycharm
# author: hzc
import multiprocessing as mp
import time
 
 
class test:
    def __init__(self, x):
        self.x = x
 
    def printf(self):
        print(self.x)
 
 
 
def foo_pool(x, y):
    time.sleep(5)
    instance = test(x * x + y)
    return instance
 
 
result_list = []
 
 
def log_result(result):
    # This is called whenever foo_pool(i) returns a result.
    # result_list is modified only by the main process, not the pool workers.
    # result_list.append(result)
    print(type(result))
 
 
def apply_async_with_callback():
    pool = mp.Pool(1)
    for i in range(10):
        pool.apply_async(foo_pool, args=(i, 2), callback=log_result)
    pool.close()
    pool.join()
    print(result_list)
 
 
if __name__ == '__main__':
    apply_async_with_callback()

解决办法
我在网上找了很久都没有找到解决方案

由于考虑到是pycharm的问题

因而改变了一下pycharm的版本(从2020.2.3->2018.3.7)

运行结果正常
如下:

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持vb.net教程C#教程python教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值