无法在 PythonWin 中运行任何多处理脚本

发布于 2024-10-09 10:35:27 字数 2126 浏览 2 评论 0原文

我已经更新了这个问题,以在多处理脚本中显示我的问题,该脚本不是从 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

我在网上找不到与此问题相关的任何内容。 感谢您的帮助!

阿米特 附: 我在 Windows XP、PythonWin、Python 2.6.4v 上运行它

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
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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-10-16 10:35:27

您需要将要运行的代码保存在 .py 文件中。多处理不支持执行仅在交互模式下输入的代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文