33

I liked the ability to turn a function into a thread without the unnecessary line to define a class. I know about _thread, however it appears that you are not supposed to use _thread. Is there a good-practice equivalent of thread.start_new_thread for python 3?

asked Jun 12, 2011 at 0:00

2 Answers 2

53
threading.Thread(target=some_callable_function).start()

or if you wish to pass arguments,

threading.Thread(target=some_callable_function,
 args=(tuple, of, args),
 kwargs={'dict': 'of', 'keyword': 'args'},
 ).start()
answered Jun 12, 2011 at 0:03
Sign up to request clarification or add additional context in comments.

4 Comments

I am using method 2 to pass one argument to a thread, and am getting the error: argument after * must be sequence, not it. Do you know what this means?
Make sure you're passing (firstarg,) not (firstarg) - remember that single-element tuples need the trailing comma to be interpreted as a tuple.
What would the callable method signature look like? If I use something like callable(**kwargs): I get TypeError: callable() takes 0 positional arguments but 1 was given, and if I use callable(kwargs): I get TypeError: callable() got an unexpected keyword argument 'raw'.
@henrikstroem threading.Thread(target=callable,kwargs=kwargs)
6

Unfortunately there is not a direct equivalent, because Python 3 is meant to be more portable than Python 2 and the _thread interface is seen as too low-level for this purpose.

In Python 3 the best practice is usually to use threading.Thread(target=f...). This uses different semantics, but is preferred because the interface is easier to port to other Python implementations.

answered Jun 12, 2011 at 0:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.