每当我使用eventlet的猴子补丁(Flask-SocketIO必需)时,disk_monitor_thread()会阻止其他线程被触发。 Eventlet和猴子补丁对我来说是必须的。有没有办法让pyudev的MonitorObserver放置好并用猴子补丁发布GIL?
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
Starting Disk Monitor...
并且永远不会从那里前进