我已更新此问题以在多处理脚本中显示我的问题,该脚本不从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加载文件” 我的文件名在哪里有完整路径。
当我从命令提示符运行相同的脚本时,它似乎运行完美而没有任何问题。当我从命令提示符运行它时,我得到以下输出: -
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
我在网上找不到与此问题有关的任何内容。 谢谢你的帮助!!
阿米特 P.S: 我在Windows XP,PythonWin,Python 2.6.4v
上运行它答案 0 :(得分:4)
您需要将要运行的代码保存在.py文件中。多处理不支持执行仅在交互模式下输入的代码。