Telepot中的线程保存串行连接(Python)

时间:2018-11-08 14:25:44

标签: python-3.x thread-safety python-multithreading telegram-bot telepot

我有一个定期输出日志数据的串行设备(Arduino),其数据将写入日志文件中。该设备还通过串行接收自发命令。我通过Telegram将命令发送到Raspberry,由Telepot处理并发送到arduino,后者在单独的线程中运行。

如何确保两个过程相互配合?

我是多线程领域的完整入门者。 这是我的代码的简化版:

import time
import datetime
import telepot
import os
import serial
from time import sleep

ser = None
bot = None


def log(data):
    with open('logfile', 'w') as f:
        file.write("Timestamp" + data)

#The handle Function is called by the telepot thread, 
#whenever a message is received from Telegram
def handle(msg):
        chat_id = msg['chat']['id']
        command = msg['text']
        print( 'Command Received: %s' % command)
        if command = '/start':
            bot.sendMessage(chat_id, 'welcome')
        elif command == 'close_door':
            #This serial write could possibly happen while a 
            #ser.readline() is executed, which would crash my program. 
            ser.write("Close Door")
        elif command == 'LOG':
            #Here i should make sure that nothing 
            #is waiting from the Arduino
            #so that the next two Serial lines are the Arduinos 
            #respoonce to the "LOG" command.
            #and that hanlde is the only 
            #function talking to the Serial port now.
            ser.write("LOG")
            response = ser.readline()
            response += "\0000000A" + ser.readline()
            #The Arduinos response is now saved as one string 
            #and sent to the User.
            bot.sendMessage(chat_id, response)

        print("Command Processed.")


bot = telepot.Bot('BOT TOKEN')
bot.message_loop(handle)


ser = serial.Serial("Arduino Serial Port", 9600)
print( 'I am listening ...')

while True:
    #anything to make it not run at full speed (Recommendations welcome)
    #The log updates are only once an hour. 
    sleep(10)

    #here i need to make sure it does not collide with the other thread.
    while ser.in_waiting > 0:
        data = ser.readline()
        log(data)

该代码不是我的实际代码,但它应该完全代表我要执行的操作。

我最后的选择是将串行代码放入线程循环函数中,但这将需要我更改丑陋的库。

我查找了一些有关Asincio中的Queue和锁定功能的内容。但是,我不太了解如何应用。而且我不使用异步电传。

1 个答案:

答案 0 :(得分:0)

在阅读了有关锁定和线程的更多信息之后,借助此问题中提供的链接,我找到了一个答案:Locking a method in Python? 通常建议使用队列,但是我不知道如何。

我的解决方案(代码可能有错误,但原理可行)

import time
import random
import datetime
import telepot
import os
import serial
from time import sleep
#we need to import the Lock from threading
from threading import Lock

ser = None
bot = None



def log(data):
    with open('logfile', 'w') as f:
        file.write("Timestamp" + data)


#create a lock:
ser_lock = Lock()


#The handle Function is called by the telepot thread, 
#whenever a message is received from Telegram
def handle(msg):
    #let the handle function use the same lock:
    global ser_lock

    chat_id = msg['chat']['id']
    command = msg['text']
    print( 'Command Received: %s' % command)
    if command == '/start':
        bot.sendMessage(chat_id, 'welcome')
    elif command == 'close_door':
        #This serial write could possibly happen while a 
        #ser.readline() is executed, which would crash my program.
        with ser_lock:
            ser.write("Close Door")
    elif command == 'LOG':
        #Here i should make sure that nothing 
        #is waiting from the Arduino
        #so that the next two Serial lines are the Arduinos 
        #respoonce to the "LOG" command.
        #and that hanlde is the only 
        #function talking to the Serial port now.

        #the lock will only be open when no other thread is using the port.
        #This thread will wait untill it's open.
        with ser_lock:
            while ser.in_waiting > 0:
                data = ser.readline()
                log(data)
                #Should there be any old data, just write it to a file
            #now i can safely execute serial writes and reads.
            ser.write("LOG")
            response = ser.readline()
            response += "\0000000A" + ser.readline()
        #The Arduinos response is now saved as one string 
        #and sent to the User.
        bot.sendMessage(chat_id, response)

    print("Command Processed.")


bot = telepot.Bot('BOT TOKEN')
bot.message_loop(handle)


ser = serial.Serial("Arduino Serial Port", 9600)
print( 'I am listening ...')

while True:
    #anything to make it not run at full speed (Recommendations welcome)
    #The log updates are only once a 
    sleep(10)

    #here i need to make sure it does not collide with the other thread.
    with ser_lock:
        while ser.in_waiting > 0:
            data = ser.readline()
            log(data)