读取串行端口读取中的子过程 [英] Subprocess in Reading Serial Port Read

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

问题描述

我正在使用python代码与我的设备之一进行串行端口通信.它正在发送一组十六进制代码,接收一组数据.处理它.

Hi I am using Serial port communication to one of my device using python codes. It is sending a set of Hex code, Receives a set of data. process it.

此数据必须存储在数据库中.

This data has to be stored in to a database.

我还有另一个脚本,该脚本具有MYSQLdb库将其推入数据库.

I have another script that has MYSQLdb library pushing it in to the database.

如果我在一个脚本中按顺序执行此操作,则会损失很多采样率.如果我不连接到数据库并将其插入表中,则每秒最多可以采样32个数据集.

If I do that in sequentially in one script i lose a lot in sampling rate. I can sample up to 32 data sets per second if I dont connect to a database and insert it in to the table.

如果我使用Multiprocessing并尝试运行它,则采样率将达到0.75,因为父进程正在等待子进程加入.所以我该如何处理这种情况.

If I use Multiprocessing and try to run it my sampling rate goes to 0.75, because the parent process is waiting for the child to join. so how can i handle this situation.

是否可以通过使用队列填充数据来独立运行它们?

Is it possible to run them independently by using a queue to fill data?

推荐答案

使用线程和队列.

这是一个示例:

from Queue import Queue
import threading
import serial

def readline(ser,output):
    threadLock.acquire()# Get lock to synchronize threads
    output.put(ser.readline())
    threadLock.release()# Free lock to release next thread
    # this will go on forever until you kill the program

if __name__=='__main__':
    output = Queue()
    with serial.Serial(com,baudrate=115200,timeout=1) as ser:
        ser_thread = threading.Thread(target=readline, args=(ser,output,))
        ser_thread.start()
        while(True):
             threadLock.acquire()#wait until lock is free
             try:
                 data=output.get()
             threadLock.release()# Free lock to release next thread
             # do somthing with data

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

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