7

Trying to get my head around threading. In my code, it's only firing one thread, when I think it should go straight on to the second. I've been reading about locks and allocating but don't quite understand. What would I need to do here to let 2 threads run independently at the same time?

import thread

def myproc(item):
    print("Thread fired for " + item)
    while True:
        pass

things = ['thingone', 'thingtwo']

for thing in things:
    try:
        thread.start_new_thread(myproc(thing))

    except:
        print("Error")
1
  • You'll need to show actual and expected output for anybody to be able to answer your question. Commented Mar 30, 2016 at 12:12

2 Answers 2

5

You've got the signature to start_new_thread wrong. You're calling myproc and passing the result as an argument to start_new_thread, which never happens as myproc never terminates.

Instead, it should be:

thread.start_new_thread(myproc, (thing,) )

The first argument is the function (ie. the function object, not calling the function) and the second is a tuple of arguments.

See: https://docs.python.org/2/library/thread.html#thread.start_new_thread

Once you have your program actually starting both threads, you probably want to add a pause at the end because the threads will terminate when the main thread finishes.

Also, please consider using the threading module instead of the thread module, as it provides a much nicer higher level interface, such as a convenient way for waiting until your threads have finished executing.

See: https://docs.python.org/2/library/threading.html#module-threading

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

3 Comments

The script also quits before the threads have the opportunity to print anything
The original script in the question didn't - it didn't get as far as starting the threads. But yes, that is a concern when you fix the original problem.
Brilliant - that makes sense!
2

Your application quits before the second thread has had time to finish, from what I can tell.

You need to wait for both of your threads to finish before your application terminates, like this:

#!/usr/bin/python

import thread
import time

# Define a function for the thread
def print_time(threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows
try:
   thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
   print "Error: unable to start thread"

while 1: # This is a bad solution due to the busy wait loop. More below.
   pass

You should preferrably store the thread objects and use thread1.join() and thread2.join() at the end before quitting to signal that they have both terminated.

2 Comments

This is useful too - how would I go about storing the threads when they are created in a loop? I would need to get the variable 'thing' into the instance name somehow?
@user4893295 See this page tutorialspoint.com/python/python_multithreading.htm under "Creating Thread Using Threading Module" for information on how to store a thread object!

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.