Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python多线程随机行为_Python_Multithreading - Fatal编程技术网

Python多线程随机行为

Python多线程随机行为,python,multithreading,Python,Multithreading,我有一个多线程python应用程序,它对文件进行大量处理 主应用程序正在扫描目录以查找新文件。只要找到一个文件,就会在其他Python线程中处理该文件(图像/视频编辑、FTP上载、将元数据保存到DB…)。这些处理器中的很大一部分都在用Popen运行外部命令,并等待它们返回 当我处理一个文件时,我没有任何问题,一切正常,但当主应用程序运行时,它会导致随机行为,我最终会出现一系列错误,如Popen命令不返回、FTP服务器断开连接等 以下是我的应用程序的简化版本,用于说明: class VideoEd

我有一个多线程python应用程序,它对文件进行大量处理

主应用程序正在扫描目录以查找新文件。只要找到一个文件,就会在其他Python线程中处理该文件(图像/视频编辑、FTP上载、将元数据保存到DB…)。这些处理器中的很大一部分都在用Popen运行外部命令,并等待它们返回

当我处理一个文件时,我没有任何问题,一切正常,但当主应用程序运行时,它会导致随机行为,我最终会出现一系列错误,如Popen命令不返回、FTP服务器断开连接等

以下是我的应用程序的简化版本,用于说明:

class VideoEditor(Thread):
    def __init__(self, file, manager):
        Thread.__init__(self);
        self.manager = manager
        self.file = file


    def run(self):
        Popen('some video editing command').wait()

        self.manager.on_video_editing_finished(self)

class FileUploader(Thread):
    def __init__(self, file, manager):
        Thread.__init__(self);
        self.manager = manager
        self.file = file

    def run(self):
        # Uploading the file to ftp server

        self.manager.on_upload_finished(self)

class Manager():
    def __init__(self):
        self.video_editors = []
        self.uploaders = []

    def on_upload_finished(self, uploader):
        self.uploaders.remove(uploader)
        file = uploader.file
        print "Processing finished for %s" % file


    def on_video_editing_finished(self, video_editor):
        self.video_editors.remove(video_editor)
        file = video_editor.file
        u = FileUploader(file, self)
        u.start()
        self.uploaders.append(u)

    def scan_and_process(self):
        # Detect new file
        ve = VideoEditor(new_file, self)
        ve.start()
        self.video_editors.append(ve)

if __name__ == "__main__":
    manager = Manager()
    while True:
        manager.scan_and_process()
        sleep(60)
有更好的设计吗?我做错什么了吗

编辑:创意 这样会更好吗

def process_file(file):
    Popen('some video editing command').wait()
    Popen('some other command').wait()
    # Upload to FTP here

class Manager():
    def scan_and_process(self):
        # Detect new file
        Popen(['python', 'Manager.py', '-process_file', new_file])

if __name__ == "__main__":
    if len(argv) == 3 and argv[1] == 'process_file':
        process_file(argv[2])
    else:
        manager = Manager()
        while True:
            manager.scan_and_process()
            sleep(60)

谢谢

您不需要线程,因为您已经在使用子进程。只需让子进程运行并用于检查完成情况。

您有线程,但它们可能没有多线程。满足需求。@delnan:这对I/O绑定进程来说很少重要。@delnan:我编辑了我的原始帖子,提出了一个摆脱GIL的建议,你认为值得一试吗?感谢“我有一个多线程python应用程序,它对文件进行大量处理。”这是一个坏主意。所有线程都在等待I/O请求。如果您正在进行文件处理,请使用
多处理
。不是线程。