无法在PythonWin中运行任何多进程脚本
我更新了这个问题,想展示我在一个多进程脚本中遇到的问题。这个脚本在PythonWin中按F5运行时不工作,但在命令提示符下却能正常运行。
"""
File Name: simple multiprocess example.py
Description:
A very basic multiprocessing script to show the use of daemon.
There are two processes:-
p1 - calls listen() and runs in the background (daemon = True)
p2 - calls write()
"""
import multiprocessing
import time
import sys
def listen():
p = multiprocessing.current_process()
p_name = str(p.name)
pid = str(p.pid)
while 1:
print "%s process with PID %s running: %s" % (p_name, pid, time.asctime())
time.sleep(1)
print 'Exiting :', p.name, p.pid
def write():
p = multiprocessing.current_process()
p_name = str(p.name)
pid = str(p.pid)
for x in xrange(3):
print "%s process with PID %s running: %s" % (p_name, pid, time.asctime())
time.sleep(1)
print 'Exiting :', p.name, p.pid
if __name__ == '__main__':
p1 = multiprocessing.Process(name='listen', target=listen)
p1.daemon = True
p2 = multiprocessing.Process(name='write', target=write)
p2.daemon = False
p1.start()
p2.start()
time.sleep(7)
当我在PythonWin中按F5运行上面的脚本时,会弹出一个消息框(标题是“Python for Win32”),提示“无法从 -c 加载文件”,这里的 -c 后面跟着的是我的文件名和完整路径。
但是,当我在命令提示符下运行同样的脚本时,它似乎完全正常,没有任何问题。我在命令提示符下运行时得到的输出是:-
s>"simple multiprocess example.py"
listen process with PID 4564 running: Tue Jan 04 09:32:49 2011
write process with PID 3744 running: Tue Jan 04 09:32:49 2011
listen process with PID 4564 running: Tue Jan 04 09:32:50 2011
write process with PID 3744 running: Tue Jan 04 09:32:50 2011
listen process with PID 4564 running: Tue Jan 04 09:32:51 2011
write process with PID 3744 running: Tue Jan 04 09:32:51 2011
listen process with PID 4564 running: Tue Jan 04 09:32:52 2011
Exiting : write 3744
listen process with PID 4564 running: Tue Jan 04 09:32:53 2011
listen process with PID 4564 running: Tue Jan 04 09:32:54 2011
listen process with PID 4564 running: Tue Jan 04 09:32:55 2011
我在网上找不到与这个问题相关的任何信息。谢谢大家的帮助!!
Amit
附言:我是在Windows XP上运行,使用的是PythonWin和Python 2.6.4版本。
1 个回答
4
你需要把想要运行的代码保存到一个 .py 文件里。因为 multiprocessing 这个模块不支持直接执行在交互模式下输入的代码。