如何在python中将tcp服务器放在另一个线程上
我尝试用 python 编写一个守护进程。但我不知道如何使用线程在这个守护进程中启动并行 tcp 服务器。甚至我应该使用什么类型的服务器:asyncore?SocketServer?socket?
这是我代码的一部分:
import os
def demonized():
child_pid = os.fork()
if child_pid == 0:
child_pid = os.fork()
if child_pid == 0: #fork twice for demonize
file = open('###', "r") # open file
event = file.read()
while event:
#TODO check for changes put changes in list variable
event = file.read()
file.close()
else:
sys.exit(0)
else:
sys.exit(0)
if __name__ == "__main__":
demonized()
所以在一个循环中,我有一个列表变量,每个循环都附加了一些数据,我想用 tcp 服务器启动一个线程,在循环中等待连接,如果客户端连接,则发送该数据(使用归零变量)。所以我不需要处理多个客户端,客户端一次只有一个。实现这一点的最佳方法是什么?
谢谢。
I try to write a daemon in python. But I have no idea how can I use a thread to start parallel tcp server in this daemon. And even what type of server I should use : asyncore?SocketServer?socket?
this is part of my code:
import os
def demonized():
child_pid = os.fork()
if child_pid == 0:
child_pid = os.fork()
if child_pid == 0: #fork twice for demonize
file = open('###', "r") # open file
event = file.read()
while event:
#TODO check for changes put changes in list variable
event = file.read()
file.close()
else:
sys.exit(0)
else:
sys.exit(0)
if __name__ == "__main__":
demonized()
So in a loop I have a list variable with some data appended every circle, and I want to start a thread with tcp server that wait for connection in the loop and if client connects send it this data(with zeroing variable). So I do not need to handle multiple clients, the client will be only one at time. What is the optimal way to implement this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想避免重复样板文件,Python 很快就会有一个标准模块,它可以执行 fork() 对和标准 I/O 操作(您还没有将其添加到程序中吗?)使其成为守护进程。您现在可以从以下位置下载并使用此模块:
http://pypi.python.org/pypi /python-daemon
在单独的线程中运行 TCP 服务器通常很简单:
我强烈建议反对尝试让文件读取器线程和套接字应答线程与您自己设计的列表和锁定;这样的计划很难开始发挥作用,也很难继续发挥作用。相反,请使用标准库的 Queue.Queue() 类,它会为您正确执行所有锁定和附加操作。
In case you want to avoid repeating boilerplate, Python will soon have a standard module that does the
fork()
pair and standard-I/O manipulations (which you have not added to your program yet?) that make it a daemon. You can download and use this module right now, from:http://pypi.python.org/pypi/python-daemon
Running a TCP server in a separate thread is often as simple as:
I strongly recommend against trying to get your file-reader thread and your socket-answering thread talking with a list and lock of your own devising; such schemes are hard to get working and hard to keep working. Instead, use the standard library's
Queue.Queue()
class which does all of the locking and appending correctly for you.您是否想在
while event:...
循环中将项目附加到列表并同时提供该列表?如果是这样,那么你有两个作家,你必须以某种方式保护你的名单。在示例中使用了
SocketServer.TCPServer
和threading.Lock
:Do you want to append items to the list in
while event:...
loop and serving this list simultaneously? If so then you have two writers and you must somehow protect your list.In the sample
SocketServer.TCPServer
andthreading.Lock
was used: