开发者

What happened to thread.start_new_thread in python 3

开发者 https://www.devze.com 2023-03-12 20:57 出处:网络
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-

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 pyt开发者_如何学Gohon 3?


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()


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号