Comments (2)
I wrote some simple code to monitor devices with asyncio. Posting it here incase it's useful:
from asyncio import Event, get_event_loop
from typing import AsyncGenerator
from pyudev import Context, Device, Monitor
async def iter_monitor_devices(context: Context, **kwargs) -> AsyncGenerator[Device, None]:
for device in context.list_devices(**kwargs):
yield device
monitor = Monitor.from_netlink(context)
monitor.filter_by(**kwargs)
monitor.start()
fd = monitor.fileno()
read_event = Event()
loop = get_event_loop()
loop.add_reader(fd, read_event.set)
try:
while True:
await read_event.wait()
while True:
device = monitor.poll(0)
if device:
yield device
else:
read_event.clear()
break
finally:
loop.remove_reader(fd)
Example:
from asyncio import run
async def monitor_usb()
context = Context()
async for device in iter_monitor_devices(context, subsystem='usb'):
print(device)
run(monitor_usb())
from pyudev.
@garyvdm Thanks for this code. It's just what I need at work <3
from pyudev.
Related Issues (20)
- 0.22.0: sphinx warnigs and errors HOT 3
- How to Iterate a device's interfaces and rejecting it HOT 2
- How to add a single allow/block rule to udev's rules?
- How to robustly monitor device addition and removal? HOT 2
- Exception AttributeError: "'Context' object has no attribute '_libudev'" in <bound method Context.__del__ of <pyudev.core.Context object at 0x6b922870>> ignored HOT 4
- Messy tagging HOT 2
- Fix PyPy CI errors, test-related CI generally
- Bump the version number?
- Fix up configuration file and Makefile release target so that they are useful again
- No read permissions for .egg-info files in PyPI tarball HOT 3
- Add typing hints?
- Test suite fails against systemd 250 HOT 2
- Remove from __future__ everywhere
- Create a release for Python2 syntax fixes
- Invoke GLib.io_add_watch() with wrong parameters HOT 1
- USB doesn't detect removal HOT 2
- UnicodeDecodeError when reading GPT PartitionName HOT 4
- AttributeError: module 'collections' has no attribute 'abc' HOT 1
- Cannot create Context() HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyudev.