Cannot run any multiprocessing script in PythonWin
I have updated this question to show my problem in a multiprocessing script that doesn't run from PythonWin (by pressing F5), but runs from command prompt. My script:-
"""
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)
When I run the above script from PythonWin (by pressing F5), a popup message comes up (with title "Python for Win32") saying "Could not load the file from -c" where is my file name with full path.
When I run the same script from command prompt, it seems to run perfectly without any trouble. I get the following output when I run it from command prompt:-
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
Exiti开发者_如何学Cng : 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
I couldn't find anything online related to this problem. Thanks for the help!!
Amit P.S: I am running it on Windows XP, PythonWin, Python 2.6.4v
You need to save the code you want to run in a .py file. multiprocessing does not support execution of code that was merely entered in the interactive mode.
精彩评论