多处理不创建新进程
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我在这里缺少一些库,所以我不得不删除你的记录器代码,因为这对我来说是一个问题。对我来说,您似乎存在命名/路径冲突。请确保名称“sslserver”不会与 python 路径中的任何模块冲突。
另外,设置python路径!在我的示例中,这两个文件位于同一目录中。
pytest.py
sslserver.py
输出
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
sslserver.py
Output