0

I have this code:

def runner1(): os.system('python something.py') ..... and so on thread_run_1 = threading.Thread(target=runner1, args=[]) .... i =0 j = 0 while i < 2: # i want to run continuously, dont want to use cron jobs print "while 1" #i made it simpler, but here are a lot of checks while j == 0: print "while 2" thread_run_1.start() ...... time.sleep(3600) break print "back to while 1" i+=1 print i 

It's possible to avoid this error: RuntimeError: threads can only be started once?

3
  • Why not create a new thread? Commented Jun 17, 2014 at 8:36
  • @bereal True. But I wanted to know if its possible directly from code. Commented Jun 17, 2014 at 8:39
  • 1
    What do you mean by "directly from code"? You would create a new thread directly from code. Python threads are thin wrappers around the OS threads, so when a thread is stopped, it no longer exists. Bereft of life, it rests in peace. It's an ex-thread. Start a new one. Commented Jun 17, 2014 at 8:45

2 Answers 2

1

You can prevent this by checking via isAlive before instantiating the Thread object. Spelling will vary depending on version. Relevant doc here.

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

Comments

1

Why are you trying to restart a thread ? Since the constructor will get worker argument, then it seems logical that that's the only thing that will ever run on that thread. If what you want to do is trigger the target to process another piece of info or just run the job again, you could :

a) Build another thread and run that (i don't know how expensive that is memory wise)

b) Make your target get a reference to a Queue object and pass information for processing into that from the main thread.

If you're trying to find something similar to java runnables or callables I don't think python offers such a construct

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.