0

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

1
  • Runnable code would be very helpful. I'm pretty sure falsetru's on the right track, as shown your work() is called in the main thread, the spawned thread would be trying to run a callable returned from work(), which isn't what you want here. Commented Jul 27, 2013 at 16:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, I haven't really explained it in code properly the instructions are received ok and processed, however, when the "do work" instruction is received I try to spawn a thread, the spawning of the thread seems successful but the thread has focus rather than returning to recieve() if you know what I mean
I dropped () and we have success, thanks. However, I'm using signal, which won't run in threads lol. Back to the drawing board

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.