Python多线程/多个进程读取串行端口 [英] Python multiple threads/ multiple processes for reading serial ports

查看:1745
本文介绍了Python多线程/多个进程读取串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个python类使用并行处理/线程读取两个串行端口(/ dev / ttyS1和/ dev / ttyS2)。这两个端口都以19200波特率运行,并且始终处于活动状态。我使用pySerial为此目的。

I'm trying to write a python class utilizing parallel processing/threading for reading two serial ports(/dev/ttyS1 and /dev/ttyS2). Both of these ports are running at a 19200 baud rate and are constantly active. I used pySerial for this purpose.

这两个读取操作需要连续和并发运行。我想知道是否应该使用线程库或线程库或多处理库。我只是担心,因为全局解释器锁不能给重的IO操作的真正的线程能力。但是如果全局解释器锁不影响我,那么我将使用线程/线程模块。但是如果这样,我需要交叉编译python多处理库,因为这是在嵌入式系统上。

Both of the read operations need to be run continuously and concurrently. I am wondering if should use the thread library or threading library or the multiprocessing library. I'm only worried because of the global interpreter lock which doesnt' give true threading ability for heavy IO operations. But if the global interpreter lock doesn't affect me then I will use the threading/thread module. However if it does then I would need to cross compile the python multiprocessing libraries because this is on an embedded system.

所以我的代码通常有
thread1或process1 =读取ttyS1,并在读取行上执行一些字符串操作后写入缓冲区。
thread2或process2 =读取ttyS2并在读取行上执行一些字符串操作后写入另一个缓冲区。
其他函数等
这些缓冲区被代码中的其他部分进一步使用。

So my code would typically have thread1 or process1 = reading ttyS1 and writing to a buffer after performing some string operations on the read lines. thread2 or process2 = reading ttyS2 and writing to another buffer after performing some string operations on the read lines. Other functions etc These buffers are further utilized by other parts in the code.

在Python中多处理也需要多个核心/ cpus?

Also does multiprocessing in python require multiple cores/cpus?

感谢阅读!

推荐答案

GIL在读取操作期间释放,因此它不应该影响你。
交叉编译 multiprocessing 听起来像过度,或至少是过早的优化。

The GIL is released during read operations, so it shouldn't affect you much. Cross-compiling multiprocessing sounds like overkill, or at least premature optimalization. Do keep the code modular so you can switch later, though.

我相信线程的性能将取决于你的操作系统。

I do believe the threading performance will depend on your OS. Your mileage will vary, especially on an embedded system.

如果您有一个小时的时间,有一个 David Beazley谈论GIL (PDF幻灯片 here )。对于高性能线程,你会想看到它得到关于线程,GIL和操作系统可以一起工作以消除性能的令人讨厌的细节。

If you have an hour to spare, there's a talk on the GIL by David Beazley (PDF slides here). For high-performance threading, you'll want to see it to get the nasty details on how threading, the GIL, and the OS can all work together to kill performance.

这篇关于Python多线程/多个进程读取串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆