【发布时间】:2012-10-12 09:57:24
【问题描述】:
我即将使用 Python 启动一个程序,该程序主要进行轮询,它将不断地从串行端口(通过PySerial)读取并从不时更改的文件描述符中读取。我开始研究threading 模块,但后来我不断发现more 和more 建议使用multiprocessing 模块。
我并不精通 Python,主要来自 C 背景。 Python 中的线程方法有哪些技术优势?
在 C 中,线程共享数据而不是必须设置一些 IPC 来进行通信,这对于 Python 来说似乎是一样的吗?
我的用例:
Main process (or thread?) -
start & initialize
|
V
spaw child----------------------> start & initialize
| |
V V
while (1) <------------+ wait for data<------+
| | | |
V | V |
read file descriptors | read from |
| | serial port<-----+ |
V | | | |
value changed? ------No--+ V | |
| ^ message done?--No-+ |
V | | |
Report change------------+ V |
over serial alert parent---------+
所以我在考虑线程,因为它可以更轻松地通过串行共享数据,并且它们可以拥有串行端口的共享句柄。这是有道理的吗,还是我从 Python 的角度考虑不正确?
【问题讨论】:
-
多处理是一种(恕我直言有点笨拙)回避 GIL 的方法。在这种情况下,我认为
threading是要走的路。 -
@Downvoter - 我有什么理由让你觉得这个问题:“没有显示出研究工作;不清楚或没有用”?
标签: python multithreading serial-port multiprocessing