Skip to content
This repository was archived by the owner on Nov 23, 2019. It is now read-only.
This repository was archived by the owner on Nov 23, 2019. It is now read-only.

Asynchronous API #33

Open
Open

Description

@pepijndevos

The kernel interface is very much asynchronous. You write a value, and stuff happens.
Currently, when you want to wait for a sensor or motor, you have to poll it.

I'm currently using this snippet in my code, but maybe we should think about a proper API, possibly using asyncio, Twisted, Gevent, or something else.

from ev3 import lego, ev3dev
import time
import select
import os

p = select.poll()
callbacks = {}

def poll(timeout):
    for fd, mask in p.poll(timeout):
        os.lseek(fd, 0, os.SEEK_SET)
        state = os.read(fd, 16)
        if state == "idle\n":
            p.unregister(fd)
            os.close(fd)
            callbacks.pop(fd)()

def on_completion(motor, callback):
    fd = os.open(motor.sys_path + '/state', os.O_RDONLY)
    callbacks[fd] = callback
    os.read(fd, 16)
    p.register(fd, select.POLLPRI)

Activity

  1. topikachu commented on Dec 26, 2014

    @topikachu
    Owner

    Well, an event based system is possible, however, the low level ev3dev system has no such capability as far as I know. Please check ev3dev/ev3dev#104 . So that means either pull by the framework or application. To avoid the unnecessary overhead, I decided to let application to poll the status. After ev3dev system support event, python-ev3 can easy support too. However, I will keep this thread open until we get a work event system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions