【问题标题】:Python Watchdog spawn multiprocessingPython 看门狗产生多处理
【发布时间】:2016-05-20 19:05:11
【问题描述】:

我正在将文件(以非常快的速度)写入特定目录。我想监视目录中的新文件,然后生成一个运行外部脚本的进程。现在,我遇到了

的酸洗错误(即使我使用的是 Pathos)
Can't pickle <type 'Struct'>: it's not found as __builtin__.Struct

我需要帮助解决酸洗错误,这可能导致我不得不重新考虑我在做什么,这很好。

这是我目前所拥有的:

#!/usr/bin/python

import os
import sys
import argparse
import json
import time
import os
from datetime import datetime
#Test for Pathos
from pathos.multiprocessing import ProcessingPool as Pool
from multiprocessing import cpu_count
from subprocess import check_output
import ConfigParser
import logging
#WatchDog
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, FileSystemMovedEvent

from CodernityDB.database import Database

###
# CONFIGURATION
###

CONFIG GOES HERE BUT REMOVED 

###
# Custom handler for Python Watchdog
# When spawned, it will spawn a new worker into the pool
###
class MyHandler(FileSystemEventHandler):
        def __init__(self):
                self.db = Database("/var/db/test.db")
                try:
                        self.db.open()
                except Exception, e:
                        print str(e)
                        self.db.create()

        def on_created(self, event):
                #print event.src_path
                try:
                        pool.map(doIt, (self.db, event.src_path,))
                except Exception, e:
                        print str(e)

def codernityIt(db, json_):
        try:
                print json_
                db.insert(json_)
        except Exception, e:
                print str(e)
                logging.error(str(e))

def doIt(db, file_):
        try:
                codernityIt(db, json.loads(check_output(['python', '/external/script.py', file_])))
        except Exception, e:
                print str(e)
                logging.error(str(e))

if __name__ == '__main__':
        ###
        # Pool specific Settings
        ###
        pool = Pool(processes=cpu_count())
        event_handler = MyHandler()
        ###
        # Watchdog specific settings
        ###
        observer = Observer()
        observer.schedule(event_handler, path=watchPath, recursive=True)
        observer.start()

        ###
        # This While True loop listens for Keyboard interrupts and will gracefully exit the program if found
        ###
        try:
                while True:
                        time.sleep(1)
        except KeyboardInterrupt:
                observer.unschedule_all()
                observer.stop()
                db.close()
        #observer.join()

【问题讨论】:

  • 我不知道如何修复你的酸洗错误,但是如果你想观察一个目录,你可以考虑通过另一个脚本来做,并反复检查 os.listdir() 的输出文件变化。如果您的用例足够简单,它的效率会更低,但更简单。

标签: python multiprocessing pickle pathos


【解决方案1】:

你为什么不试试inotify? 可能对你有帮助:https://pypi.python.org/pypi/inotify

【讨论】:

  • 我最终更改了我的脚本以使用 PYINotify 并且它可以工作。谢谢!
【解决方案2】:

我很确定它失败了,因为dillpathos 使用)不知道如何腌制Struct。它是我认为 dill 目前无法处理的少数对象之一,因为它没有定义在命名空间中的位置……请参阅:https://github.com/python/typeshed/issues/24

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多