# Function triggered when data comes to serial port in python

I am checking the incoming data by reading continuously in while true in a separate thread. I don't want it to work constantly. Is there a function that will only be triggered when data arrives in  python?

https://bit.ly/3iZ3cZD

my code:

```import threadingimport serial

connected = Falseport = "COM3"baud = 115200serial_port = serial.Serial(port, baud, timeout=0)

def handle_data(data):
print(data)

def read_from_port(ser):        select_read(serial_port)        serin = ser.read()        connected = True

while True:           reading = serial_port.in_waiting
reading = ser.readline().decode()
if len(reading) > 5:
handle_data(reading)

def set_data():
while True:
print("input: ")        num1 = str(input())        serial_port.write(num1.encode());thread = threading.Thread(target=read_from_port, args=(serial_port,))thread2 = threading.Thread(target=set_data)thread.start()thread2.start()```
0 Replies
{"ft_lang_label":"__label__en","ft_lang_prob":0.6339278,"math_prob":0.64916027,"size":819,"snap":"2021-21-2021-25","text_gpt3_token_len":191,"char_repetition_ratio":0.1582822,"word_repetition_ratio":0.0,"special_character_ratio":0.25641027,"punctuation_ratio":0.19424461,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9541732,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-16T12:33:49Z\",\"WARC-Record-ID\":\"<urn:uuid:6a5f02ff-bc5a-46d5-91be-3dac371f1fd6>\",\"Content-Length\":\"414691\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2d553e50-02fa-41ed-9ef2-163b3a2b4427>\",\"WARC-Concurrent-To\":\"<urn:uuid:8b6d15e5-9402-4973-8459-26f1bcbd7a4b>\",\"WARC-IP-Address\":\"23.47.20.113\",\"WARC-Target-URI\":\"https://techcommunity.microsoft.com/t5/it-resources-training/function-triggered-when-data-comes-to-serial-port-in-python/td-p/1664130\",\"WARC-Payload-Digest\":\"sha1:M7UNIOYC6FNM44KAGBFHAUGKNCH2TQPL\",\"WARC-Block-Digest\":\"sha1:5SS7MCUDWURS6PB3VFI747PE5AWTYFQJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487623596.16_warc_CC-MAIN-20210616093937-20210616123937-00038.warc.gz\"}"}