Spawning a new thread while importing

瘦欲@ 提交于 2019-12-07 23:48:14

问题


Python's threading documentation states:

Other than in the main module, an import should not have the side effect of spawning a new thread and then waiting for that thread in any way. Failing to abide by this restriction can lead to a deadlock if the spawned thread directly or indirectly attempts to import a module.

I'm looking for example code that demonstrates this restriction.


回答1:


I tried this, a module that spawns a thread whose target tries to import sys:

from threading import Thread

def my_target():
    import sys

thread = Thread(target=my_target)
thread.start()
thread.join()

When the python interpreter is launched and an attempt to import the module above is made, it indeed freezes.



来源:https://stackoverflow.com/questions/8456389/spawning-a-new-thread-while-importing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!