簡體   English   中英

如何暫停Python中的所有其他線程?

[英]How can I pause all the other threads in Python?

我正在制作一個"python debug mapper" ,它顯示當前python執行的'snapshot'

目前,我需要知道一種方法來pause every other threads以便在其他線程運行時不會發生'capture'

有什么方法可以做:

  • PauseOtherThreads();
  • ResumeOtherThreads();

謝謝。

ps:我應該進行任何修改以使代碼與Celery和Django一起使用嗎?

根據您是否只想在其他線程正在運行時跟蹤一個線程,或者如果您想要停止其他線程,我可以考慮使用兩個解決方案。 如果其他線程必須在沒有跟蹤的情況下運行,只需讓trace命令首先檢查當前線程id,如果該線程是您感興趣的線程,則僅執行跟蹤操作:

def dotrace():
    if tracing and threading.current_thread() == the_traced_thread:
        ... do the tracing ...

如果其他線程在跟蹤其他線程時必須停止,則可以使跟蹤操作停止為其他線程添加如下內容:

def dotrace():
    while tracing and threading.current_thread() != the_traced_thread:
        time.sleep(0.01)
    if tracing and threading.current_thread() == the_traced_thread:
        ... do the tracing ...

當然,只有跟蹤操作才能在最后一種情況下停止運行,因此其他線程可能會一直運行直到完成或執行任何跟蹤操作。

基本上,您只會停止正在監視的其他線程而不是所有其他線程。 我說這很好,因為增加了程序仍然保持功能的可能性(你使用的一些庫和框架可能需要其他線程來運行以跟蹤實際工作的線程),當然還有YMMV。

你可以使用sys.setcheckinterval(somebignumber)

setcheckinterval(...)
    setcheckinterval(n)

    Tell the Python interpreter to check for asynchronous events every
    n instructions.  This also affects how often thread switches occur.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM