Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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_Multiprocessing_Python Multithreading_Watchdog - Fatal编程技术网

将python看门狗与多处理或线程相结合

将python看门狗与多处理或线程相结合,python,multiprocessing,python-multithreading,watchdog,Python,Multiprocessing,Python Multithreading,Watchdog,我正在使用Python监视给定目录中正在创建的新文件。创建文件时,会运行一些代码,生成子流程shell命令,以运行不同的代码来处理此文件。这应该为创建的每个新文件运行。我已经在创建一个文件时对此进行了测试,效果很好,但是在创建多个文件时,无论是同时创建还是一个接一个创建,我都很难让它正常工作 我现在的问题是。。。在shell中运行的处理代码需要一段时间才能运行,并且在目录中创建新文件之前不会完成。对此我无能为力。当该代码运行时,看门狗将无法识别已创建的新文件,并且不会继续执行该代码 所以我认为我

我正在使用Python监视给定目录中正在创建的新文件。创建文件时,会运行一些代码,生成子流程shell命令,以运行不同的代码来处理此文件。这应该为创建的每个新文件运行。我已经在创建一个文件时对此进行了测试,效果很好,但是在创建多个文件时,无论是同时创建还是一个接一个创建,我都很难让它正常工作

我现在的问题是。。。在shell中运行的处理代码需要一段时间才能运行,并且在目录中创建新文件之前不会完成。对此我无能为力。当该代码运行时,看门狗将无法识别已创建的新文件,并且不会继续执行该代码

所以我认为我需要为每个新文件生成一个新的进程,或者做一些事情让事情并发运行,而不是等到一个文件完成后再处理下一个文件

因此,我的问题是:

1.)实际上,我将在一个目录中同时创建4个不同系列的文件。让watchdog一次为所有4个文件创建文件时运行代码的最佳方法是什么

2.)当代码针对一个文件运行时,如何让watchdog开始处理同一系列中的下一个文件,而不必等待上一个文件的处理完成。这是必要的,因为文件是特定的,我需要暂停一个文件的处理,直到另一个文件完成,但它们的创建顺序可能会有所不同

我是否需要以某种方式将我的看门狗与多处理或线程结合起来?还是我需要实现多个观察者?我有点不知所措。谢谢你的帮助

class MonitorFiles(FileSystemEventHandler):
    '''Sub-class of watchdog event handler'''

    def __init__(self, config=None, log=None):
        self.log = log
        self.config = config

    def on_created(self, event):
        file = os.path.basename(event.src_path)
        self.log.info('Created file {0}'.format(event.src_path))
        dosWatch.go(event.src_path, self.config, self.log)

    def on_modified(self, event):
        file = os.path.basename(event.src_path)
        ext = os.path.splitext(file)[1]
        if ext == '.fits':
            self.log.warning('Modifying a FITS file is not allowed')
            return

    def on_deleted(self, event):
        self.log.critical('Nothing should ever be deleted from here!')
        return      
主要监测
在上面,我的monitor方法设置了watchdog来监视主目录。MonitorFiles类定义创建文件时发生的操作。它基本上调用dosWatch.go方法,该方法最终调用subprocess.Popen来运行shell命令

我不确定对每个文件执行一个线程是否有意义。这样做可能会消除您从中看到的任何优势,甚至可能严重影响性能并导致一些意外行为。我个人认为,
watchdog
不是很可靠。您可能会考虑实现自己的文件观察器,这可以像在Django框架(参见)中相当容易地完成,通过为每个文件创建具有修改的时间戳的DICT。

< P>这是我最后做的,这解决了我的问题。我使用多处理来启动一个单独的看门狗监视进程,以分别监视每个文件。Watchdog已经为我排队等待新文件,这对我来说很好

至于上面的第2点,我需要在文件1之前处理文件2,即使文件1是首先创建的。因此,在file1期间,我检查file2处理的输出。如果找到它,它将继续处理file1。如果没有,它就会退出。在处理file2时,我检查file1是否已经创建,如果已经创建,则处理file1。(此代码未显示)

摄像机的主要监控 启动多个摄影机进程
难道你不能直接生成子流程而不等待它们完成吗?我以为这就是我要做的。这个dosWatch.go调用了一些东西,但最终运行了一个p=subprocess.Popen(cmd,shell=shell,stdout=subprocess.PIPE,stderr=subprocess.stdout),它运行一些其他代码。我不想等到它完成,但出于某种原因它正在这样做。我在某个地方读到,watchdog基本上是线程的一个子类,observer.join()是让进程等待直到完成的罪魁祸首。但是我对线程包知之甚少。在
watchdog
内部,有一个线程调用
FileSystemEventHandler
对象。因此,是的,您是正确的,在这个线程中按顺序调用了*上的
。如果要在工作完成之前接收下一个事件,则需要生成一个线程,并立即从创建的
返回。确保dosWatch.go真的不是block`on_创建的)实际上它并没有我想象的那么糟糕。“看门狗”会对事件进行排队,这对我来说是再合适不过了。我只需要添加一些代码来处理一些文件排序。然后我将整个过程包装在一个多处理命令中,生成一个看门狗进程来监视4种类型的文件。目前它似乎运行良好。是的,我可能不需要每个文件都有一个线程。但我确实需要为我的4个单独的文件中的每一个进程。这些需要同时处理,相互独立。每个组中的后续文件可以在相同的线程上排队。我曾想过实现我自己的文件监视程序,但似乎有人已经比我做得更好了。不过,我会看看这个django的例子;你能帮忙吗
def monitor(config, log):
    '''Uses the Watchdog package to monitor the data directory for new files.
    See the MonitorFiles class in dosClasses for actual monitoring code'''

    event_handler = dosclass.MonitorFiles(config, log)

    # add logging the the event handler
    log_handler = LoggingEventHandler()

    # set up observer
    observer = Observer()
    observer.schedule(event_handler, path=config.fitsDir, recursive=False)
    observer.schedule(log_handler, config.fitsDir, recursive=False)
    observer.start()
    log.info('Begin MaNGA DOS!')
    log.info('Start watching directory {0} for new files ...'.format(config.fitsDir))

    # monitor
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.unschedule_all()
        observer.stop()
        log.info('Stop watching directory ...')
        log.info('End MaNGA DOS!')
        log.info('--------------------------')
        log.info('')
    observer.join() 
def monitorCam(camera, config, mainlog):
    '''Uses the Watchdog package to monitor the data directory for new files.
    See the MonitorFiles class in dosClasses for actual monitoring code.  Monitors each camera.'''

    mainlog.info('Process Name, PID: {0},{1}'.format(mp.current_process().name,mp.current_process().pid))

    #init cam log
    camlog = initLogger(config, filename='manga_dos_{0}'.format(camera))
    camlog.info('Camera {0}, PID {1} '.format(camera,mp.current_process().pid))
    config.camera=camera

    event_handler = dosclass.MonitorFiles(config, camlog, mainlog)

    # add logging the the event handler
    log_handler = LoggingEventHandler()

    # set up observer
    observer = Observer()
    observer.schedule(event_handler, path=config.fitsDir, recursive=False)
    observer.schedule(log_handler, config.fitsDir, recursive=False)
    observer.daemon=True
    observer.start()
    camlog.info('Begin MaNGA DOS!')
    camlog.info('Start watching directory {0} for new files ...'.format(config.fitsDir))
    camlog.info('Watching directory {0} for new files from camera {1}'.format(config.fitsDir,camera))

    # monitor
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.unschedule_all()
        observer.stop()
        camlog.info('Stop watching directory ...')
        camlog.info('End MaNGA DOS!')
        camlog.info('--------------------------')
        camlog.info('')
    #observer.join()

    if observer.is_alive():
        camlog.info('still alive')
    else:
        camlog.info('thread ending')    
def startProcess(camera,config,log):
    ''' Uses multiprocessing module to start 4 different camera monitoring processes'''

    jobs=[]

    #pdb.set_trace()

    #log.info(mp.log_to_stderr(logging.DEBUG))
    for i in range(len(camera)):
        log.info('Starting to monitor camera {0}'.format(camera[i]))
        print 'Starting to monitor camera {0}'.format(camera[i])
        try:
            p = mp.Process(target=monitorCam, args=(camera[i],config, log), name=camera[i])
            p.daemon=True
            jobs.append(p)
            p.start()
        except KeyboardInterrupt:
            log.info('Ending process: {0} for camera {1}'.format(mp.current_process().pid, camera[i]))
            p.terminate()
            log.info('Terminated: {0}, {1}'.format(p,p.is_alive()))

    for i in range(len(jobs)):
        jobs[i].join()  

    return