Pyjo.Reactor.Poll - Low-level event reactor with poll support

from Pyjo.Reactor.Poll import Poll

# Watch if handle becomes readable or writable
reactor = Poll()
reactor.io(first, lambda reactor, writable: print(
    'First handle is writable' if writable else 'First handle is readable'
))

# Or import the module, as in Google Python Style Guide

import Pyjo.Reactor.Poll

# Watch if handle becomes readable or writable
reactor = Pyjo.Reactor.Poll.new()
reactor.io(first, lambda reactor, writable: print(
    'First handle is writable' if writable else 'First handle is readable'
))

# Change to watching only if handle becomes writable
reactor.watch(first, False, True)

# Watch a file descriptor if it becomes readable
reactor.io(fd, lambda reactor, writable: print(
    'Second handle is writable' if writable else 'Second handle is readable'
)).watch(fd, True, False)

# Add a timer
def timeout(reactor):
    reactor.remove(first)
    reactor.remove(fd)
    print('Timeout!')

reactor.timer(15, timeout)

# Start reactor if necessary
if not reactor.is_running:
    reactor.start()

Pyjo.Reactor.Poll is a low-level event reactor based on select.poll(), without asyncio. Windows has no select.poll(), and select.select() takes its place there, which watches sockets only, not pipes or files, up to 512 of them. PYJO_REACTOR selects it for Pyjo.IOLoop:

PYJO_REACTOR=Pyjo.Reactor.Poll

Events

Pyjo.Reactor.Poll inherits all events from Pyjo.Reactor.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Reactor::Poll of Mojolicious 9.49.

class Pyjo.Reactor.Poll.Pyjo_Reactor_Poll[source]

Pyjo.Reactor.Poll inherits all methods from Pyjo.Reactor and implements the following new ones.

again(id: str, after: float | None = None) → None[source]
reactor.again(id)
reactor.again(id, 0.5)

Restart timer and optionally change the invocation time. Note that this method requires an active timer.

io(handle: Handle, cb: IOCallback) → Self[source]
reactor = reactor.io(handle, cb)

Watch handle for I/O events, invoking the callback whenever handle becomes readable or writable.

# Callback will be executed twice if handle becomes readable and writable
reactor.io(handle, lambda reactor, writable: print('Handle is writable' if writable else 'Handle is readable'))
property is_running: bool[source]
boolean = reactor.is_running

Check if reactor is running.

next_tick(cb: TimerCallback) → None[source]
none = reactor.next_tick(cb)

Execute callback as soon as possible, but not before returning or other callbacks that have been registered with this method, always returns None.

one_tick() → None[source]
reactor.one_tick()

Run reactor until an event occurs or no events are being watched anymore.

# Don't block longer than 0.5 seconds
id = reactor.timer(0.5, lambda reactor: None)
reactor.one_tick()
reactor.remove(id)
recurring(after: float, cb: TimerCallback) → str[source]
id = reactor.recurring(0.25, cb)

Create a new recurring timer, invoking the callback repeatedly after a given amount of time in seconds.

remove(remove: Handle | str) → bool[source]
boolean = reactor.remove(handle)
boolean = reactor.remove(id)

Remove handle or timer.

reset() → None[source]
reactor.reset()

Remove all handles and timers.

start() → None[source]
reactor.start()

Start watching for I/O and timer events, this will block until stop() is called or no events are being watched anymore.

# Start reactor only if it is not running already
if not reactor.is_running:
    reactor.start()
stop() → None[source]
reactor.stop()

Stop watching for I/O and timer events.

timer(after: float, cb: TimerCallback) → str[source]
id = reactor.timer(0.5, cb)

Create a new timer, invoking the callback after a given amount of time in seconds.

watch(handle: Handle, read: bool, write: bool) → Self[source]
reactor = reactor.watch(handle, readable, writable)

Change I/O events to watch handle for with true and false values. Note that this method requires an active I/O watcher.

# Watch only for readable events
reactor.watch(handle, True, False)

# Watch only for writable events
reactor.watch(handle, False, True)

# Watch for readable and writable events
reactor.watch(handle, True, True)

# Pause watching for events
reactor.watch(handle, False, False)