in python multithreading, only the main thread can receive the signal and cannot use join blocking.
so how does the main thread end the child thread in the blocking state after receiving the signal
the following code child threads receive_task1
are blocking in the os.read ()
(receiving serial port data, but the serial port does not receive data) function. Is there any way to let the os.read ()
function exit?
if you use os.read ()
in the main thread, by default you can use Ctrl+C
to interrupt. What"s the best way to do this now?
import os
import signal
import threading
import time
def test(signum, frame):
print("\nreceived sig %d" % (signum))
print("how to make os.read(...) in rcv_task1 thread stop? ")
print("except use sys.exit(1)")
def receive_task1():
fd = os.open("/dev/ttyUSB1", os.O_RDWR)
os.read(fd, 10)
print("receive thread end")
signal.signal(signal.SIGINT, test)
rcv_task1 = threading.Thread(target=receive_task1)
rcv_task1.setDaemon(True)
rcv_task1.start()
while(1):
time.sleep(1)