V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tukey
V2EX  ›  Python

Python 多进程的程序代码 Linux 中可以执行,但是 win7 中用 pycharm 调试报错

  •  1
     
  •   tukey · 2018-03-31 16:59:47 +08:00 · 5107 次点击
    这是一个创建于 2190 天前的主题,其中的信息可能已经有所发展或是发生改变。

    from multiprocessing import Process

    def text(): print("--子进程--")

    p = Process(target=text) p.start() p.join() print("--主进程--")

    在 Ubuntu 中执行没有问题 在 win7 下,pycharm 中运行 报错如下: Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\anaconda\envs\py36\lib\multiprocessing\spawn.py", line 105, in spawn_main exitcode = _main(fd) File "C:\anaconda\envs\py36\lib\multiprocessing\spawn.py", line 113, in _main preparation_data = reduction.pickle.load(from_parent) _pickle.UnpicklingError: invalid load key, '\x00'.

    请大家指点我一下,这个问题搜了好久没找到。

    12 条回复    2018-03-31 18:18:12 +08:00
    tukey
        1
    tukey  
    OP
       2018-03-31 17:00:47 +08:00
    编辑的时候排版好的,发布就乱了,sorry.
    fslong
        2
    fslong  
       2018-03-31 17:16:17 +08:00
    刚好我昨天才学到这里,由于 Windows 没有 fork 调用,在 Linux 上能用的多进程代码,在 Windows 上会报错。Windows 上可以使用 multiprocessing,详见: https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319272686365ec7ceaeca33428c914edf8f70cca383000
    tukey
        3
    tukey  
    OP
       2018-03-31 17:19:27 +08:00   ❤️ 1
    @fslong 谢谢,这个知识点我在廖雪峰老师那里学了,我在使用 multiprocessing 时也遇到了问题,_pickle.UnpicklingError: invalid load key, '\x00'.
    SErHo
        4
    SErHo  
       2018-03-31 17:24:46 +08:00 via iPhone
    放在 if main 里面跑试试。
    zjqzxc
        5
    zjqzxc  
       2018-03-31 17:28:34 +08:00
    if __name__ == '__main__':
    加到 p = Process(target=text) 之前
    zjqzxc
        6
    zjqzxc  
       2018-03-31 17:29:30 +08:00
    原因:
    https://segmentfault.com/q/1010000003869715

    “这是 Windows 上多进程的实现问题。在 Windows 上,子进程会自动 import 启动它的这个文件,而在 import 的时候是会执行这些语句的。如果你这么写的话就会无限递归创建子进程报错。所以必须把创建子进程的部分用那个 if 判断保护起来,import 的时候 __name__ 不是 __main__ ,就不会递归运行了。”
    tukey
        7
    tukey  
    OP
       2018-03-31 17:43:45 +08:00
    @zjqzxc 你好,我重新写的如下
    from multiprocessing import Process
    def text():
    print("子进程")
    if __name__ == "__main__":
    p = Process(target=text)
    p.start()
    p.join()
    print("主进程")

    也有问题
    tukey
        8
    tukey  
    OP
       2018-03-31 17:45:09 +08:00
    @SErHo 放了试了下,还是报错,我换个 Python 的版本试一下。
    wwqgtxx
        9
    wwqgtxx  
       2018-03-31 18:01:26 +08:00
    @tukey 刚用 win10 1709 和 py3.6 试了你 7#的代码,没问题的
    tukey
        10
    tukey  
    OP
       2018-03-31 18:05:21 +08:00
    @wwqgtxx 好的谢谢,我用的 win7,在 Ubuntu 平台上跑没问题,可能 win 有点问题。
    wwqgtxx
        11
    wwqgtxx  
       2018-03-31 18:07:22 +08:00
    @tukey 检查两个地方,第一个是不是控制台字符集冲突问题,你 print 英文试试,第二个,看你用的 anaconda,你可以试试原版的 cpython 是否正常
    tukey
        12
    tukey  
    OP
       2018-03-31 18:18:12 +08:00
    @wwqgtxx 试了下,在 cmd 里用命令 python+py 文件执行没有问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2894 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 73ms · UTC 14:17 · PVG 22:17 · LAX 07:17 · JFK 10:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.