你如何阻止pyudev MonitorObserver用eventlet阻塞主线程?

时间:2016-08-04 02:03:54

标签: python monkeypatching eventlet flask-socketio pyudev

**更新

我有以下内容,根据pyudev readthedocs网站上的示例,它应该是守护进程线程。但是,即使将它与线程耦合,它也会在使用eventlet的猴子补丁时阻塞。

from threading import Thread
from pyudev import Context, Monitor, MonitorObserver

import eventlet
eventlet.monkey_patch()

def useless_thread():
    while True:
        print 'sleep thread 1'
        time.sleep(2)

# Monitor UDEV for drive insertion / removal
def disk_monitor_thread():
    context = Context()
    monitor = Monitor.from_netlink(context)
    monitor.filter_by('block')
    def print_device_event(action, device):
        if 'ID_FS_TYPE' in device and device.get('ID_FS_UUID') == '123-UUIDEXAMPLE':
            print('{0}, {1}'.format(device.action, device.get('ID_FS_UUID')))
    print 'Starting Disk Monitor...'
    observer = MonitorObserver(monitor, print_device_event, name='monitor-observer')
    print 'Disk Monitor Started'
    observer.start()

t1 = Thread(name='uselessthread', target=useless_thread)
t1.start()
disk_monitor_thread()

运行:

sleep thread 1
'Disk Monitor Started'

我正在使用针对Flask-SocketIO的eventlet猴子补丁,这导致useless_thread1()停止射击。如果我注释掉猴子补丁电话,那么一切都按预期工作。

0 个答案:

没有答案