亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python檢查在不同類中啟動的線程

python檢查在不同類中啟動的線程

富國滬深 2022-06-07 16:44:04
我很好奇如何獲取已在 python 中的單獨類中啟動的線程的狀態。所以目前我有:class VideoCapture:     def record:         Thread(name='uploading', target=self.upload, args=(upload_queue)).start()在一個單獨的文件main.py中,我有一個VideoCapture. 我希望能夠通過鍵入類似VideoCapture.uploading.isAlive(). 但是我收到 VideoCapture 沒有上傳對象的錯誤。那么我怎樣才能訪問它呢?
查看完整描述

2 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

將線程存儲為類的一部分,然后您可以稍后訪問它。


class VideoCapture:

    def __init__():

        self.uploading = None

    def record:

       self.uploading = Thread(name='uploading', target=self.upload, args=(upload_queue)).start()

現在在其他地方你有:


video_capture = VideoCapture()

video_capture.record()

if video_capture.uploading.isAlive(): 

    # do something


查看完整回答
反對 回復 2022-06-07
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

對象中有方法is_alive(),Thread所以基本上你只需要使用它:


class VideoCapture:

    def record(self):

        # don't forget to add it on __init__

        self.uploading = Thread(name='uploading', target=self.upload, args=(upload_queue)).start()


tmp = VideoCapture()

tmp.record()

tmp.uploading.is_alive()  # here it is


查看完整回答
反對 回復 2022-06-07
  • 2 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號