将导入/使用任何python的内置库产生线程而不被明确询问?是否有任何python内置模块使用线程?
回答
multiprocessing
模块和subprocess
模块都在内部产生threading.Thead
对象以帮助处理它们产生的进程的I/O。
具体来说,multiprocessing.Pool
spawns three threads只要你实例化它:
class Pool(object):
'''
Class which supports an async version of the `apply()` builtin
'''
Process = Process
def __init__(self, processes=None, initializer=None, initargs=(),
maxtasksperchild=None):
... # Stuff we don't care about
self._worker_handler = threading.Thread(
target=Pool._handle_workers,
args=(self,)
)
self._worker_handler.daemon = True
self._worker_handler._state = RUN
self._worker_handler.start()
self._task_handler = threading.Thread(
target=Pool._handle_tasks,
args=(self._taskqueue, self._quick_put, self._outqueue, self._pool)
)
self._task_handler.daemon = True
self._task_handler._state = RUN
self._task_handler.start()
self._result_handler = threading.Thread(
target=Pool._handle_results,
args=(self._outqueue, self._quick_get, self._cache)
)
self._result_handler.daemon = True
self._result_handler._state = RUN
self._result_handler.start()
subprocess
生成线程when you call popen_object.communicate
,从运行子读取标准输出/标准错误。
def _communicate(self, input):
stdout = None # Return
stderr = None # Return
if self.stdout:
stdout = []
stdout_thread = threading.Thread(target=self._readerthread,
args=(self.stdout, stdout))
stdout_thread.setDaemon(True)
stdout_thread.start()
if self.stderr:
stderr = []
stderr_thread = threading.Thread(target=self._readerthread,
args=(self.stderr, stderr))
stderr_thread.setDaemon(True)
stderr_thread.start()
编辑
Mark Dickinson points out该concurrent.futures.ProcessPoolExecutor
还产生一个线程,为类似于multiprocessing.Pool
原因(处理I/O与产卵过程):
def _start_queue_management_thread(self):
# When the executor gets lost, the weakref callback will wake up
# the queue management thread.
def weakref_cb(_, q=self._result_queue):
q.put(None)
if self._queue_management_thread is None:
# Start the processes so that their sentinels are known.
self._adjust_process_count()
self._queue_management_thread = threading.Thread(
target=_queue_management_worker,
args=(weakref.ref(self, weakref_cb),
self._processes,
self._pending_work_items,
self._work_ids,
self._call_queue,
self._result_queue))
self._queue_management_thread.daemon = True
self._queue_management_thread.start()
对于Python 3,'concurrent.futures'和'asyncio'可能值得一提。 –
@MarkDickinson就我所知,使用'concurrent.futures'你总是明确地要求一个线程(例如使用'ThreadExecutor')。我不认为'asyncio'创建任何线程;它只是有能力与'concurrent.futures.ThreadExecutor'集成。当您没有明确询问时,OP专门寻找使用线程的地方。 – dano
是的,我想这取决于“明确”是如何明确的。一个带有'concurrent.futures'的稍微隐藏的例子就是创建一个'ProcessPoolExecutor':当你第一次向它提交一个工作时,你会在你的主进程中得到一些额外的线程(这要感谢你使用'multiprocessing'机制已经确定,所以也许这不算)。同意'asyncio'。 –
下列模块进口threading
,表示可以使用“线程”(无论是隐式还是明示)
asyncio
decimal
functools
cookiejar
multiprocessing
queue
sched
subprocess
telnetlib
tempfile
trace
方法论
grep -r "thread" * | grep "import" | grep -v "test" | grep -v "Lib/threading"
在{python install path}/Lib
目录。
使用的方法是查看grep结果中是否出现“线程”,并使用一系列grep
来处理结果。
所以拿一点盐的这个答案。
是的,其中大部分只是导入它来获得同步原语。我认为唯一一个实际产生的线程在我的回答中没有提到,它是'telnetlib',它使用'thread.start_new_thread'而不是'threading'模块。但是只有在你明确地用'mt_interact'(意思是多线程交互)来提问的时候才会这样做。 – dano
- 1. Python的搁置模块是否使用内存映射IO?
- 2. 是否有任何Python模块可用于[{},{} ...]比较?
- 3. Linux内核模块是否有任何内存限制?
- 4. 是否有python模块来求解线性方程组?
- 5. 是否有任何理由不要在Python中混合使用多处理和线程模块
- 6. 使用Python内置模块检查进程是否在Windows中运行
- 7. Python线程模块
- 8. 是否有任何良好的python css内联模块在野外的HTML?
- 9. 是否有OCaml ppx_xml_conv模块的任何使用示例
- 10. 是否有使用django的i18n的任何帮助模块?
- 11. 打印所有的Python内建子模块和内置模块
- 12. 在Python中是否有任何基于事件的编程模块
- 13. .NET模块是否线程安全?
- 14. 是否有任何方式或需要使用Unity3d的线程?
- 15. Python线程模块创建用户空间线程或内核特殊线程
- 16. Python中的线程与线程模块
- 17. Python是否有脚本命令行任务模块?
- 18. 学习Python线程模块
- 19. Python线程管理模块
- 20. Python的线程模块
- 21. 在Python 3中是否使用模块线程和多处理相互排斥?
- 22. 使用线程和队列模块的Python中的多线程
- 23. 使用线程/线程模块的好处python 2.7
- 24. 使用线程模块在python中实现线程
- 25. 使用QThread和线程模块进行Python多线程处理
- 26. 读取内置Python模块
- 27. 是否需要在同步块内使用线程wait()和notify()?
- 28. 是否有Pygame模块的在线Python解释器?
- 29. 是否有Python内置Python编辑器?
- 30. 在线程模块中使用python中的Eventlet模块有什么好处?
在导入时创建线程应该很少;进口时产生的副作用通常被忽视。但是,似乎'import tkinter'在我的OS X机器上创建了一个新的线程。 –
当然''进口反引力'开始一个新的*进程*! (如果您的网络浏览器尚未运行。) –