user2625871
user2625871

Reputation: 3

python threads, multithreading with threading.thread

I have to launch a thread in the background but the output seems to be following the thread rather than sticking within main. I have something like this:

import threading

def work()
  while True:
  do stuff

def recieve()
  while True:
  await instruction

#main

recieve()
if instruction == "do work"
  threading.Thread(target=work()).start()

I have many other instructions that get recieved and dealt with sequentially but as work() takes a long time to complete I need the thread, now, I would expect a thread to be launched that does work in the background whilst we continue to await instruction except this doesn't happen. What happens is focus is kept on the newly created thread so further instructions can't be received.

Why is this? What is wrong?

Many thanks

Upvotes: 0

Views: 365

Answers (1)

falsetru
falsetru

Reputation: 369324

receive() never end because of endless loop; thread does not start.

Start thread first.

if instruction == "do work":
    threading.Thread(target=work).start()
recieve()

and drop () from threading.Thread(target=work()).start(). work() make work function call run in main thread.

Upvotes: 1

Related Questions