0
import time
from threading import Thread

def s_process():
    print('***********************************************')
    ##time.sleep(2)
    print('###############################################')
    ##time.sleep(2)
    return

a = Thread(target=s_process)

while(True):
    a.start()
    a.join()
    a.start()
    a.join() 

why is this code causing an error

***********************************************
###############################################
Traceback (most recent call last):
  File "xxxxxxxxxxxxxxxxxxxxx", line 16, in <module>
    a.start()
RuntimeError: threads can only be started once

shouldn't join() wait till the thread is finished. and if I have misunderstood how join() works how should I wait for the thread to finish without using a timeout

  • Change your code to this ** while(True): a = Thread(target=s_process) a.start() a.join() ** – Stack Jun 15 '17 at 14:06
  • 1
    The error isn't on the `join` line, it's on the `start` line. It seems self-explanatory to me: don't call `start` twice on the same object. Create a new thread object if you have to. – Kevin Jun 15 '17 at 14:06
  • You are defining only 1 thread, `a` and you already started plus called its `join()` method. Cant start it again! – pstatix Jun 15 '17 at 14:07
  • Is it safe to create so many thread objects? would they mess with the memory usage ? or do they get cleaned up after finishing – confusedsnek Jun 15 '17 at 14:14
  • So threads use the same memory footprint, where as processes get their own footprint. The Global Interpreter Lock (or GIL) allows only one thread at a time to execute Python bytecode (unless its I/O and then _most_ of it bypasses the GIL). I recommend reading [this](https://stackoverflow.com/questions/1294382/what-is-a-global-interpreter-lock-gil) and [that](http://www.dabeaz.com/python/UnderstandingGIL.pdf) for some more in-depth knowledge. – pstatix Jun 15 '17 at 14:18

2 Answers2

0

This should work :

import time
from threading import Thread

def s_process():
    print('***********************************************')
    ##time.sleep(2)
    print('###############################################')
    ##time.sleep(2)
    return

while(True):
    a = Thread(target=s_process)
    a.start()
    a.join()
    del a           #deletes a
Stack
  • 4,116
  • 3
  • 18
  • 23
0

To start multiple threads, build a list of threading.Thread objects and iterate their start() and join() methods using a for loop like so:

import time
from threading import Thread

def s_process():
    print('***********************************************')
    time.sleep(2)
    print('###############################################')
    time.sleep(2)
    return

# list comprehension that creates 2 threading.Thread objects
threads = [Thread(target=s_process) for x in range(0, 2)]

# starts thread 1
# joins thread 1
# starts thread 2
# joins thread 2
for thread in threads:
    try:
        thread.start()
        thread.join()
    except KeyboardInterrupt: # std Python exception
        continue # moves to next thread iterable

Edit: Inlcuded try/except for KeyboardInterrupt and tested with general use Ctrl+X+C.

pstatix
  • 3,611
  • 4
  • 18
  • 40