为什么进程无法加入并且无法运行? [英] why process doesn't join and doesn't run?

查看:73
本文介绍了为什么进程无法加入并且无法运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题要解决(或多或少)
如果我看了python多处理教程,就会发现应该像这样或多或少地启动一个过程:

i have a simple problem to solve(more or less)
if i watch python multiprocessing tutorials i see that a process should be started more or less like this:

from multiprocessing import *

def u(m):
    print(m)
    return

A=Process(target=u,args=(0,))
A.start()
A.join()

它应该打印0,但是什么也不会打印.相反,它会永远挂在A.join()上.

It should print a 0 but nothing gets printed. Instead it hangs forever at the A.join().

如果我手动启动此功能

A.run()

它实际上在shell上打印0,但不能同时工作
例如以下代码的输出:

it actually prints 0 on the shell but it doesn't work simultaneously
for example the output of following code:

from multiprocessing import *
from time import sleep

def u(m):
    sleep(1)
    print(m)
    return

A=Process(target=u,args=(1,))
A.start()
print(0)

应该是
0
1

should be
0
1

但实际上是
0

but actually is
0

如果我在最后一行之前添加

and if i add before the last line

A.run()

然后输出变为
1
0

then the output becomes
1
0

这让我感到困惑...

this seems confusing to me...

如果我尝试加入该过程,它将永远等待.

and if i try to join the process it waits forever.

但是,如果可以帮助我给出答案,
我的操作系统是Mac os x 10.6.8
使用的python版本是3.1和3.3
我的电脑有1个Intel Core i3处理器

however,if it can help giving me an answer
my OS is Mac os x 10.6.8
python versions used are 3.1 and 3.3
my computer has 1 intel core i3 processor

-更新-
我注意到只有在从IDLE启动程序时,才会出现这种奇怪的行为,如果我从终端运行程序,则一切按预期运行,因此此问题必须与某些IDLE错误相关.
但是从终端运行的程序甚至更奇怪:使用range(100000000)之类的东西可以激活我所有的计算机内存,直到程序结束;如果我记得很好,这不应该在python 3中发生,只有在较旧的python版本中才发生. 我希望这些新信息可以帮助您给出答案

--Update--
I have noticed that this strange behaviour is present only when launching the program from IDLE ,if i run the program from the terminal everything works as it is supposed to,so this problem must be connected to some IDLE bug.
But runnung programs from terminal is even weirder: using something like range(100000000) activates all my computer's ram until the end of the program; if i remember well this shouldn't happen in python 3,only in older python versions. I hope these new informations will help you giving an answer

-更新2--
即使设置了以下错误,即使我不从进程中执行输出,也会出现该错误:

--Update 2--
the bug occurs even if i don't perform output from my process,because setting this:

def u():
    return

作为该过程的目标,然后启动它,如果我尝试加入该过程,idle将永远等待

as the target of the process and then starting it , if i try to join the process,idle waits forever

推荐答案

根据建议此处此处,问题在于IDLE以某些怪异的方式覆盖sys.stdinsys.stdout,这些方式不能干净地传播到您从中生成的进程(它们不是真正的文件句柄).

As suggested here and here, the problem is that IDLE overrides sys.stdin and sys.stdout in some weird ways, which do not propagate cleanly to processes you spawn from it (they are not real filehandles).

第一个链接还表明它不可能在短期内得到修复(他们说可能是无法修复"的问题").

The first link also indicates it's unlikely to be fixed any time soon ("may be a 'cannot fix' issue", they say).

不幸的是,我唯一可以建议的解决方案是不要对此脚本使用IDLE ...

So unfortunately the only solution I can suggest is not to use IDLE for this script...

这篇关于为什么进程无法加入并且无法运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆