2

New to Python multi-thread and write such simple program, here is my code and error message, any ideas what is wrong? Thanks.

Using Python 2.7.

import time
import thread

def uploader(threadName):

    while True:
        time.sleep(5)
        print threadName

if __name__ == "__main__":

    numOfThreads = 5
    try:
        i = 0
        while i < numOfThreads:
            thread.start_new_thread(uploader, ('thread'+str(i)))
            i += 1

        print 'press any key to exit test'
        n=raw_input()

    except:
       print "Error: unable to start thread"

Unhandled exception in thread started by <pydev_monkey._NewThreadStartupWithTrace instance at 0x10e12c830>
Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_monkey.py", line 521, in __call__
    return self.original_func(*self.args, **self.kwargs)
TypeError: uploader() takes exactly 1 argument (7 given)

thanks in advance, Lin

8
  • 2
    ('thread'+str(i)) will give you a string, not a tuple. Try this: ('thread'+str(i),). Commented Mar 13, 2016 at 0:50
  • 1
    You mean you get this error: uploader() takes exactly 1 argument (7 given)? Are you sure you have saved it before re-running it? Incidentally, if you are new to python, you might find the threading module more helpful. Commented Mar 13, 2016 at 0:59
  • 2
    It seems to work for me when I change the line as @JustinBarber suggested. The threads start printing their names after sleeping for 5 secs. Commented Mar 13, 2016 at 1:05
  • 1
    @Lin Ma I have added the answer below. Glad you figured it out. Cheers. Commented Mar 13, 2016 at 1:06
  • 1
    @LinMa Good to hear :) Commented Mar 13, 2016 at 1:21

2 Answers 2

3

The args of thread.start_new_thread need to be a tuple. Instead of this:

('thread' + str(i))  # results in a string

Try this for the args:

('thread' + str(i),)  # a tuple with a single element

Incidentally, you should check out the threading module, which is a higher-level interface than thread.

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

2 Comments

Thanks for the help, Justin. Mark your reply as an answer. :)
Thanks, @Lin Ma! Glad you got it figured out.
1

In the following, threadName is now a global variable defined towards the top of the program code, then the variable is initialized before the new thread is started with the target being the upload function.

Try this:

import time
import thread

threadName = ''

def uploader():

    while True:
        time.sleep(5)
        print threadName

if __name__ == "__main__":

    numOfThreads = 5
    try:
        i = 0
        while i < numOfThreads:
            threadName = 'thread' + str(i)
            newThread = threading.Thread(target=uploader)
            newThread.start()
            i += 1

        print 'press any key to exit test'
        n=raw_input()

    except:
       print "Error: unable to start thread"

2 Comments

Code only answers, although they may be part of a complete answer, are discouraged since they do not explain what was fixed or how. For people with similar issues in the future, this may not help them resolve their own questions without asking a subsequent question.
@AlexanderHuszagh Ok I will keep that in mind when answering. I just edited the answer with a quick explanation

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.