1

I'm trying to implement a simple threadpool in python.

I start a few threads with the following code:

 threads = []
        for i in range(10):
            t = threading.Thread(target=self.workerFuncSpinner(
            taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i))
            t.setDaemon(True)
            threads.append(t)
            t.start()

        for thread in threads:
            thread.join()

At this point, the worker thread only prints when it starts and exits and time.sleeps between. The problem is, instead of getting output like:

#All output at the same time

thread 1 starting
thread 2 starting
thread n starting

# 5 seconds pass

thread 1 exiting
thread 2 exiting
thread n exiting

I get:
thread 1 starting
# 5 seconds pass
thread 1 exiting
thread 2 starting
# 5 seconds pass
thread 2 exiting
thread n starting
# 5 seconds pass
thread n exiting

And when I do a threading.current_thread(), they all report they are mainthread.

It's like there not even threads, but running in the main thread context.

Help?

Thanks

2 Answers 2

2

You are calling workerFuncSpinner in the main thread when creating the Thread object. Use a reference to the method instead:

t=threading.Thread(target=self.workerFuncSpinner, 
    args=(taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i))

Your original code:

t = threading.Thread(target=self.workerFuncSpinner(
    taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i))
t.start()

could be rewritten as

# call the method in the main thread
spinner = self.workerFuncSpinner(
    taskOnDeckQueue, taskCompletionQueue, taskErrorQueue, i)

# create a thread that will call whatever `self.workerFuncSpinner` returned, 
# with no arguments
t = threading.Thread(target=spinner)

# run whatever workerFuncSpinner returned in background thread
t.start()

You were calling the method serially in the main thread and nothing in the created threads.

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

1 Comment

This was it. I don't quite understand why the one arg version assumes main thread context, but thank you.
0

I suspect workerFuncSpinner may be your problem. I would verify that it is not actually running the task, but returning a callable object for the thread to run.

https://docs.python.org/2/library/threading.html#threading.Thread

Comments

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.