將導入/使用任何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機器上創建了一個新的線程。 –
當然''進口反引力'開始一個新的*進程*! (如果您的網絡瀏覽器尚未運行。) –