在Python解释器后台运行方法
我在 Python 3.x 中创建了一个类,充当服务器。一种方法使用 socket 模块管理通过 UDP/IP 发送和接收数据(数据存储在 self.cmd 和 self.msr 中分别)。我希望能够从 python 解释器在线修改self.msr、self.cmd变量。例如:
>>> from myserver import MyServer
>>> s = MyServer()
>>> s.bakcground_recv_send() # runs in the background, constantly calling s.recv_msr(), s.send_cmd()
>>> process_data(s.msr) # I use the latest received data
>>> s.cmd[0] = 5 # this will be sent automatically
>>> s.msr # I can see what the newest data is
到目前为止,s.bakcground_recv_send()还不存在。每次我想查看 s.msr 的值更新时,我都需要手动调用 s.recv_msr() (s.recv_msr 使用阻塞套接字),然后调用s.send_cmd()发送s.cmd。
在这种特殊情况下,哪个模块更有意义:多进程还是线程? 有什么提示我怎样才能最好地解决这个问题?我对进程或线程都没有经验(只是读了很多书,但我仍然不确定该走哪条路)。
I've made a class in Python 3.x, that acts as a server. One method manages sending and receiving data via UDP/IP using the socket module (the data is stored in self.cmd, and self.msr respectively). I want to be able to modify the the self.msr, self.cmd variables from within the python interpreter online. For example:
>>> from myserver import MyServer
>>> s = MyServer()
>>> s.bakcground_recv_send() # runs in the background, constantly calling s.recv_msr(), s.send_cmd()
>>> process_data(s.msr) # I use the latest received data
>>> s.cmd[0] = 5 # this will be sent automatically
>>> s.msr # I can see what the newest data is
So far, s.bakcground_recv_send() does not exist. I need to manually call s.recv_msr() each time I want to see update the value of s.msr (s.recv_msr uses a blocking socket), and then call s.send_cmd() to send s.cmd.
In this particular case, which module makes more sense: multiprocess or threading?
Any hints how could I best solve this? I have no experience with either processes or threads (just read a lot, but I am still unsure which way to go).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,线程最有意义。简而言之,多处理是为了在不同的处理器上运行进程,线程是为了在后台做事情。
In this case, threading makes most sense. In short, multiprocessing is for running processes on different procesors, threading is for doing things in the background.