GithubHelp home page GithubHelp logo

Add asyncio support? about pyudev HOT 2 CLOSED

igo95862 avatar igo95862 commented on August 30, 2024 1
Add asyncio support?

from pyudev.

Comments (2)

garyvdm avatar garyvdm commented on August 30, 2024

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.

askolo4ek avatar askolo4ek commented on August 30, 2024

@garyvdm Thanks for this code. It's just what I need at work <3

from pyudev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.