我不是这方面的专家,但我一直发现使用threading
需要的额外细节的数量是不值得的,如果我可以通过流程并行化。
您没有提到的替代方案中的第三个模块是subprocess
。
根据OP的要求编辑:您可以通过为串行接口创建单独的脚本来实现并行处理。这是一个快速演示,它假定两个文件都在同一个目录中。
文件com.py
- 串行脚本 - 这只是一个模拟的,但这里的想法是,脚本独立经营,并且只使用标准输入和标准输出与主控程序进行通信。
import sys
counter = 0
while True: # The program never ends... will be killed when master is over.
counter += 1
sys.stdin.readline()
sys.stdout.write('Serial from com1 is %d\n' % counter)
sys.stdout.flush()
文件master.py
- 主程序
from subprocess import Popen, PIPE
from time import sleep
p = Popen(['python', './com.py'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
print "serial communication started." # com.py is working but we moved on!
for i in range(3):
p.stdin.write('<command-here>\n')
print "comand sent."
print "received : %s" % p.stdout.readline()
sleep(1)
最后,这是预期输出的转储:
[email protected]:~/Desktop$ ./master.py
serial communication started.
comand sent.
received : Serial from com1 is 1
comand sent.
received : Serial from com1 is 2
comand sent.
received : Serial from com1 is 3
HTH!
来源
2011-11-24 17:21:00
mac
感谢有关GIL发布的信息。不适合使用的嵌入式系统将采用arm5tel架构的550 MHz CPU。我想我只是不得不做一些基准测试,看看我能从中得到多少。也感谢视频生病看看它! – kal