多处理不创建新进程

发布于 2024-11-29 06:52:16 字数 1011 浏览 1 评论 0原文

Windows 7 x86 上的 Python 3.2.1 中的多处理模块似乎打败了我。

我有两个模块:servmain.py 和 sslserver.py。这个想法是(最终)编写一个使用 SSL 与客户端通信的应用程序。这是我已经记下的部分。但是,我需要将服务器侦听器分离到其自己的单独进程中,以便主进程可以执行其他一些操作。作为虚拟测试,我告诉子进程将“Hello World”打印到标准输出并将文本写入不存在的文本文件。

这是我的父进程(servmain.py)的代码:

from multiprocessing import Process
import logging

if __name__ == "__main__":
    logger = multiprocessing.log_to_stderr()
    logger.setLevel(logging.DEBUG)

    #Fire up the server


    listenerProcess = Process(target = sslserver.startServer)
    listenerProcess.start()
    logger.debug("Starting listener.")
    listenerProcess.join()
    logger.debug("Done.")

这是 sslserver.py 代码:

def startServer():
    print("Hello World")
    f= open("testfile.txt", "w")
    f.write("Hello world\n")
    f.close()

当我运行 servmain.py 时,我得到以下输出:

[DEBUG/MainProcess] 启动侦听器。 [调试/主进程] 完成。

这正是我所期望的。但是,testfile.txt 尚未创建,并且没有输出到 stdout。有谁知道为什么会发生这种情况?

The multiprocessing module in Python 3.2.1 on Windows 7 x86 seems to be defeating me.

I have two modules: servmain.py and sslserver.py. The idea is to (eventually) code an application which will communicate with clients using SSL. This is the part I already have down. However, I need the server listener to be spun off into its own separate process so the main process can do some other stuff. As a dummy test, I told the child process to print "Hello World" to stdout and write text to a non-existent text file.

Here is the code for my parent process(servmain.py):

from multiprocessing import Process
import logging

if __name__ == "__main__":
    logger = multiprocessing.log_to_stderr()
    logger.setLevel(logging.DEBUG)

    #Fire up the server


    listenerProcess = Process(target = sslserver.startServer)
    listenerProcess.start()
    logger.debug("Starting listener.")
    listenerProcess.join()
    logger.debug("Done.")

And here is the sslserver.py code:

def startServer():
    print("Hello World")
    f= open("testfile.txt", "w")
    f.write("Hello world\n")
    f.close()

When I run servmain.py, I get the following output:

[DEBUG/MainProcess] Starting listener. [DEBUG/MainProcess] Done.

Which is what I would expect. However, testfile.txt has not been created, and there is no output to stdout. Does anyone have an idea about why this might be happening?

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

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

发布评论

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

评论(1

千秋岁 2024-12-06 06:52:16

我想我在这里缺少一些库,所以我不得不删除你的记录器代码,因为这对我来说是一个问题。对我来说,您似乎存在命名/路径冲突。请确保名称“sslserver”不会与 python 路径中的任何模块冲突。

另外,设置python路径!在我的示例中,这两个文件位于同一目录中。

pytest.py

#!/usr/bin/env python
from multiprocessing import Process
import sslserver
import logging
if __name__ == "__main__":
    #logger = multiprocessing.log_to_stderr()
    #logger.setLevel(logging.DEBUG)

    #Fire up the server


    listenerProcess = Process(target = sslserver.startServer)
    listenerProcess.start()
    #logger.debug("Starting listener.")
    print "Starting Listener\n"
    listenerProcess.join()
    #logger.debug("Done.")
    print "Done\n";

sslserver.py

#!/usr/bin/env python

def startServer():
    print("Hello World")
    f= open("testfile.txt", "w")
    f.write("Hello world\n")
    f.close()

输出

nynex@citadel:~/temp$ ./pytest.py 
Starting Listener

Hello World
Done

nynex@citadel:~/temp$ cat testfile.txt 
Hello world

I think I am missing a few libraries here so I had to remove your logger code because it was a problem for me. To me it would seem that you have a naming/pathing conflict. Please be sure the name 'sslserver' does not collide with any modules in python path.

Also, set python path! In my example both of these files are in the same directory.

pytest.py

#!/usr/bin/env python
from multiprocessing import Process
import sslserver
import logging
if __name__ == "__main__":
    #logger = multiprocessing.log_to_stderr()
    #logger.setLevel(logging.DEBUG)

    #Fire up the server


    listenerProcess = Process(target = sslserver.startServer)
    listenerProcess.start()
    #logger.debug("Starting listener.")
    print "Starting Listener\n"
    listenerProcess.join()
    #logger.debug("Done.")
    print "Done\n";

sslserver.py

#!/usr/bin/env python

def startServer():
    print("Hello World")
    f= open("testfile.txt", "w")
    f.write("Hello world\n")
    f.close()

Output

nynex@citadel:~/temp$ ./pytest.py 
Starting Listener

Hello World
Done

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