連接thread和發送信號時出現故障

我下面的代碼可以工作,但是在結束程序并再次啟動它幾次之后,行

thread.started.connect(worker.work)

不會運行,文件也不會發送到GUI。以下是問題的分類:

以下是我將兩個hdf5文件發送到文件夾時終端的常見結果:

running API pyqt5
using configuration at imageanalyser/qimageanalyser/image_analyzer.conf
no Pyro installed. Use dummy event handler.
{'coordinates': True, '_actions': {'home': <PyQt5.QtWidgets.QAction object at 0x7f8681235a60>, 'back': <PyQt5.QtWidgets.QAction object at 0x7f8681235af0>, 'forward': <PyQt5.QtWidgets.QAction object at 0x7f8681235b80>, 'pan': <PyQt5.QtWidgets.QAction object at 0x7f8681235ca0>, 'zoom': <PyQt5.QtWidgets.QAction object at 0x7f8681235dc0>, 'configure_subplots': <PyQt5.QtWidgets.QAction object at 0x7f8681235d30>, 'edit_parameters': <PyQt5.QtWidgets.QAction object at 0x7f8681235ee0>, 'save_figure': <PyQt5.QtWidgets.QAction object at 0x7f868123f040>}, 'locLabel': <PyQt5.QtWidgets.QLabel object at 0x7f8681235f70>, 'canvas': <qimageanalyser.mplwidget.MplCanvas object at 0x7f8688935940>, '_nav_stack': <matplotlib.cbook.Stack object at 0x7f868123d850>, '_lastCursor': <Cursors.POINTER: 1>, '_id_press': 2, '_id_release': 3, '_id_drag': 4, '_pan_info': None, '_zoom_info': None, 'mode': <_Mode.NONE: ''>}
try to connect to event service ...
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (3rd copy).hdf5> is locked, retrying 1/350
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (3rd copy).hdf5> is locked, retrying 2/350
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (3rd copy).hdf5> is locked, retrying 3/350
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (3rd copy).hdf5> is locked, retrying 4/350
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (3rd copy).hdf5> is locked, retrying 5/350
finished run test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (3rd copy).hdf5 2021_08_10_13_43_29 (3rd copy).hdf5
<HDF5 file "2021_08_10_13_43_29 (3rd copy).hdf5" (mode r+)>
analyze_image
xmean 135.47
xstd 41.31
xmean 148.42
xstd 41.17
mass:  3.81923985318e-26 [kg] size:  0.0002074740400778919 [m] tof:  0.01 [s]
mass:  3.81923985318e-26 [kg] size:  0.000122395198908796 [m] tof:  0.01 [s]
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (4th copy).hdf5> is locked, retrying 1/350
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (4th copy).hdf5> is locked, retrying 2/350
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (4th copy).hdf5> is locked, retrying 3/350
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (4th copy).hdf5> is locked, retrying 4/350
h5 file <test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (4th copy).hdf5> is locked, retrying 5/350
finished run test_image_analyzer_files/Test_Data/2021_08_10_13_43_29 (4th copy).hdf5 2021_08_10_13_43_29 (4th copy).hdf5
<HDF5 file "2021_08_10_13_43_29 (4th copy).hdf5" (mode r+)>
analyze_image
xmean 135.47
xstd 41.31
xmean 148.42
xstd 41.17
mass:  3.81923985318e-26 [kg] size:  0.0002074740400778919 [m] tof:  0.01 [s]
mass:  3.81923985318e-26 [kg] size:  0.000122395198908796 [m] tof:  0.01 [s]

但一旦我結束程序,然后重新啟動它,可能是3rd-5th連接無法通過的時間,下面是結果,不管我嘗試了多少次將新hdf5文件發送到文件夾:

running API pyqt5
using configuration at imageanalyser/qimageanalyser/image_analyzer.conf
no Pyro installed. Use dummy event handler.
{'coordinates': True, '_actions': {'home': <PyQt5.QtWidgets.QAction object at 0x7f8681235a60>, 'back': <PyQt5.QtWidgets.QAction object at 0x7f8681235af0>, 'forward': <PyQt5.QtWidgets.QAction object at 0x7f8681235b80>, 'pan': <PyQt5.QtWidgets.QAction object at 0x7f8681235ca0>, 'zoom': <PyQt5.QtWidgets.QAction object at 0x7f8681235dc0>, 'configure_subplots': <PyQt5.QtWidgets.QAction object at 0x7f8681235d30>, 'edit_parameters': <PyQt5.QtWidgets.QAction object at 0x7f8681235ee0>, 'save_figure': <PyQt5.QtWidgets.QAction object at 0x7f868123f040>}, 'locLabel': <PyQt5.QtWidgets.QLabel object at 0x7f8681235f70>, 'canvas': <qimageanalyser.mplwidget.MplCanvas object at 0x7f8688935940>, '_nav_stack': <matplotlib.cbook.Stack object at 0x7f868123d850>, '_lastCursor': <Cursors.POINTER: 1>, '_id_press': 2, '_id_release': 3, '_id_drag': 4, '_pan_info': None, '_zoom_info': None, 'mode': <_Mode.NONE: ''>}
try to connect to event service ...

最后是受此影響的程序代碼。我正在使用Pycharm和thread.started.connect(worker.work),特別是connect這個詞被突出顯示,警告信息是Cannot find reference 'connect' in 'function | pyqtBoundSignal'

thread = QThread(parent=self)  # this part is in the DesignerMainWindow() __init__() shown below
print('try to connect to event service ...')
worker = watchdog_search.Worker("/home/test_image_analyzer_files/Test_Data/")
worker.moveToThread(thread)
thread.started.connect(worker.work)
thread.start()
worker.new_file.connect(self.on_finished_run)


def run_app():
    try:
        # first try to get an existing app
        app = QtGui.QApplication.instance()
        reuseapp = True
        if app is None:
            app = QtGui.QApplication(sys.argv)
            reuseapp = False
    except:
        pass
    dmw = DesignerMainWindow()
    dmw.show()
    return dmw, app, reuseapp


if __name__ == "__main__":
    dmw, app, reuseapp = run_app()
    if "app" in globals() and not reuseapp:
        sys.exit(app.exec_())

以及watchdog_search文件。注意self.new_file.emit(event.src_path, os.path.basename(event.src_path))中的emit也在此處突出顯示,警告為Cannot find reference 'emit' in 'pyqtSignal | pyqtSignal'

import time
import traceback
import os

import h5py
import queue
from typing import Union

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, DirCreatedEvent, FileCreatedEvent

from .tools.qt import QtCore

from PyQt5.QtCore import pyqtSignal


from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import (
    QObject,
    QThread,
    pyqtSignal,
    pyqtSlot,
)

class NewFileHandler(FileSystemEventHandler):

    def __init__(self, q, *a, **k):
        super().__init__(*a, **k)
        self._q = q

    def on_created(self, event):
        self._q.put(event)

class Worker(QObject):

    new_file = pyqtSignal(str,str)

    def __init__(self, path):
        super().__init__()
        self._q = queue.Queue()
        observer = Observer()
        handler = NewFileHandler(self._q)
        observer.schedule(handler, path=path, recursive=True)
        # starts a background thread! Thus we need to wait for the
        # queue to receive the events in work.
        observer.start()

    def work(self):
        while True:
            event = self._q.get()
            max_retry_count = 350  # for test purposes now but want to set an upper bound on verifying a file is
            # finished.
            retry_interval_seconds = .01  # every hundreth it will try the file to see if it finished writing
            retry_count = 0
            if event.event_type == "created" and event.src_path.lower().endswith(".hdf5"):
                while True:
                    try:
                        file = h5py.File(event.src_path, "r")
                        file.close()
                    except OSError:
                        if retry_count < max_retry_count:
                            retry_count += 1
                            print(f"h5 file <{event.src_path}> is locked, retrying {retry_count}/{max_retry_count}")
                            time.sleep(retry_interval_seconds)
                        else:
                            print(f"h5 file <{event.src_path}> reached max retry count, skipping")
                            break  # <--- looks useful here
                    except Exception as err:
                        print(f"Got unexpected Error <{type(err).__name__}> while opening <{event.src_path}> ")
                        traceback.print_exc()
                    else:
                        self.new_file.emit(event.src_path, os.path.basename(event.src_path))
                        break
? 最佳回答:

我使用守護程序threading修復了這個問題:

    worker = watchdog_search.Worker("/home/test_image_analyzer_files/Test_Data/")
    worker.new_file.connect(self.on_finished_run)
    thread = threading.Thread(target=worker.work)
    thread.setDaemon(True)
    thread.start()
主站蜘蛛池模板: 国产日韩一区二区三区在线播放| 亚洲一区二区免费视频| 亚洲国产一区二区三区青草影视| 亚洲一区二区三区电影| 精品少妇人妻AV一区二区三区| 国产一区二区电影| 国产香蕉一区二区三区在线视频 | 国产亚洲综合一区柠檬导航| 国产在线步兵一区二区三区| 亚洲熟女综合色一区二区三区| 中文字幕国产一区| 加勒比精品久久一区二区三区| 亚洲欧洲专线一区| 亚洲AV日韩综合一区尤物| 91亚洲一区二区在线观看不卡| 亚洲AV无码第一区二区三区| 夜夜精品视频一区二区| 国产精品久久一区二区三区| 久久一区二区三区免费播放| 国偷自产一区二区免费视频| 99久久精品国产高清一区二区| 中文字幕一区二区三区日韩精品| 99久久无码一区人妻a黑| 伦理一区二区三区| 免费无码一区二区三区蜜桃| 国产伦精品一区二区三区视频金莲| 精品福利一区3d动漫| 国产日韩视频一区| 国产伦精品一区二区三区免费迷| 影院成人区精品一区二区婷婷丽春院影视| 日韩AV无码久久一区二区| 国产乱子伦一区二区三区| 无码人妻一区二区三区一| 亚洲一区二区三区深夜天堂| 日本一区二区视频| 国产精品一区二区综合| 国产一区二区三区在线看片| 一区二区三区视频免费观看| 99久久精品国产免看国产一区| 亚洲国产一区国产亚洲| 亚洲综合在线一区二区三区|