本文介绍了import在Python线程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一些函数交互式加载python模块使用 __ import __ 我最近偶然发现了一些关于import lock在Python中,也就是说,一个专门用于导入的锁(不仅仅是GIL)。 这让我想知道在线程中导入的实践。 是 import / __ import __ 线程安全吗? $ b $ b 编辑2012年9月12日 感谢您的回复Soravux。 所以import是线程安全的,我不担心死锁,因为在我的代码中使用 __ import __ 的函数不会相互调用。 p> 你知道即使模块已经导入,锁是否被获取? 如果是这样,我应该在sys.modules中查看模块是否已经在调用 __ import __ 之前已经被导入。 当然这不应该在CPython中有很大的区别,因为有GIL。 编辑2012年9月19日 $ b 关于Jython,这里是他们在文档中说的: http://www.jython.org/jythonbook/en/1.0/Concurrency.html#module-import-lock So, it seems that it would make sense to check in sys.modules before making an import, to avoid acquiring the lock. What do you think? 解决方案 Normal imports are thread safe because they acquire an import lock prior to execution and release it once the import is done. If you add your own custom imports using the hooks available, be sure to add this locking scheme to it. Locking facilities in Python may be accessed by the imp module (imp.lock_held()/acquire_lock()/release_lock()).Using this import lock won't create any deadlocks or dependency errors aside from the circular dependencies that are already known.The low-level call to create a thread being clone on Linux, threading in Python is then a fork-like operation. Forking and cloning applies different behaviors on the various memory segments. For exemple, only the stack is not shared by threads, compared to forks which clones more segments (Data (often COW), Stack, Code, Heap), effectively not sharing its content. The import mechanism in Python uses the global namespace which is not placed on the stack, thus using a shared segment with its threads. Since the side-effects (ie. the changes in memory) of importing works in the same segments, it behaves as a single-threaded program. Be careful to use thread safe libraries in your imports on multithreaded programs, though. It will cause mayhem to use calls to functions that are not thread safe in such environment.By the way, threaded programs in Python suffers the GIL which won't allow much performance gains unless your program is I/O bound or rely on C or external thread-safe libraries (since they release the GIL before executing). Running in two threads the same imported function won't execute concurrently because of this GIL. Note that this is only a limitation of CPython and other implementations of Python will have a different behavior.To answer your edit: imported modules are all cached by Python. If the module is already loaded in the cache, it won't be run again and the import statement (or function) will return right away. You don't have to implement yourself the cache lookup in sys.modules, Python does that for you and won't imp lock anything, aside from the GIL for the sys.modules lookup.To answer your second edit: I prefer having to maintain a simpler code than trying to optimize calls to the libraries I use (in this case, the standard library). The rationale is that the time required to perform something is usually way more important than the time required to import the module that does it. Furthermore, the time required to maintain this kind of code throughout the project is way higher than the time it will take to execute. It all boils down to: "programmer time is more valuable than CPU time". 这篇关于import在Python线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 19:37
查看更多