当前位置: 代码网 > it编程>前端脚本>Python > pycharm中多进程踩坑记录--Python

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

2024年05月18日 Python 我要评论
pycharm多进程踩坑–python所遇到的情况如下:在进程中创建类实例,回调函数去获取实例时,报如下错误,注意在cmd和linux下面运行是没问题的。执行的代码如下:# --codin

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)

运行结果正常

如下:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com