-1

I tried to send and receive at the same time using Thread, and only the thread is working, the rest of the code does not excuse.

My code:

def send():
    while True:
        time.sleep(0.5)
        print("Send Method")

thread = threading.Thread(send())
thread.start()

while True:
    try:
        time.sleep(1)
        print("Loop")
    except Exception as e:
        pass

only "Send Method" is printed

How can I make them both work?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ziv Sion
  • 424
  • 4
  • 14

1 Answers1

0

The issue is in this line

thread = threading.Thread(send())

What happens here is that your calling the send function, so it enters it the while True loop in send function, and it never comes out of that loop. To solve this give the function as parameter without brackets And you also have to specify the target parameter and give your function like this:

thread = threading.Thread(target= send)

Final code:

import threading, time

def send():
    while True:
        time.sleep(0.5)
        print("Send Method")

thread = threading.Thread(target= send)
thread.start()

while True:
    try:
        time.sleep(1)
        print("Loop")
    except Exception as e:
        pass