Pyjo.Reactor.Winloop - Low-level event reactor on Winloop¶
from Pyjo.Reactor.Winloop import Winloop
# Watch if handle becomes readable or writable
reactor = Winloop()
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.Winloop
# Watch if handle becomes readable or writable
reactor = Pyjo.Reactor.Winloop.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.Winloop is a low-level event reactor based on
Winloop, the fork of uvloop for
Windows, the implementation of the event loop of asyncio on libuv. It
is Pyjo.Reactor.Asyncio with an event loop of its own of Winloop, in
the place of the asyncio.SelectorEventLoop that
Pyjo.Reactor.Asyncio makes on Windows, so that the servers of
Pyjoyment, as Pyjo.Server.Daemon and Pyjo.Server.Prethread,
and the blocking requests of its user agent run on Winloop. PYJO_REACTOR
selects it for Pyjo.IOLoop, in PowerShell:
$env:PYJO_REACTOR = 'Pyjo.Reactor.Winloop'
python myapp.py daemon
The loop of Winloop is a loop of asyncio, so coroutines, tasks and the
libraries of asyncio run along with the reactor, as with
Pyjo.Reactor.Asyncio. It watches sockets, as the
asyncio.SelectorEventLoop of Windows does, not pipes or files, but
with the I/O completion ports of libuv, without the limit of 512 sockets of
select.select(). A reactor uses the event loop it is given, or else the
loop that is running, as inside asyncio.run(), which has to be able to
watch handles, as a loop of Winloop or an asyncio.SelectorEventLoop
is, and only a loop of its own is one of Winloop. An ASGI server that runs a
loop of Winloop itself, as with loop_factory=winloop.new_event_loop, is
taken by Pyjo.Reactor.Asyncio already, so it needs no reactor of
Winloop.
A signal handler of Python runs while a loop of its own waits, as on the
Ctrl-C or Ctrl-Break that stops a server, but the wakeup fd that a program
sets with signal.set_wakeup_fd() is not written to then, as Winloop
sets one of its own while it runs, in the main thread. A timer of a loop of
its own, as asyncio.sleep(), is never due before its time, as with the
loop of asyncio, although libuv counts it in whole milliseconds, so it
may be up to two milliseconds late.
Reactors in threads of a process that watch the same socket, as the workers
of Pyjo.Server.Prethread watch the listen sockets of the manager, take
turns: libuv of Windows cannot poll a socket for more than one loop at a time,
so the loop of one of them watches it, and hands it over to the next one after
each of its events, and when it stops running.
Winloop runs on Windows, and on CPython, not on PyPy. The
PYJO_ASYNCIO_SLOW_CALLBACK environment variable sets the
slow_callback_duration of a loop of its own in seconds, as for
Pyjo.Reactor.Asyncio.
Events¶
Pyjo.Reactor.Winloop inherits all events from Pyjo.Reactor.Asyncio.
See also¶
Pyjo.Reactor.Asyncio, Pyjoyment, https://github.com/pyjoyment/Pyjoyment.
Classes¶
- class Pyjo.Reactor.Winloop.Pyjo_Reactor_Winloop(loop: AbstractEventLoop | None = None)[source]¶
Pyjo.Reactor.Winloopinherits all methods fromPyjo.Reactor.Asyncio.
Examples
examples/hello_world_winloop.py runs Pyjo.HelloWorld on the server of
Pyjoyment on Winloop until Ctrl-C or Ctrl-Break, in a with block,
which closes the listen sockets of the server at its end:
import os # The reactor of Pyjoyment on Winloop, selected before Pyjo.IOLoop is loaded os.environ['PYJO_REACTOR'] = 'Pyjo.Reactor.Winloop' import Pyjo.HelloWorld # noqa: E402 import Pyjo.Server.Daemon # noqa: E402 # The server of Pyjoyment on Winloop, until Ctrl-C or Ctrl-Break, which closes its listen sockets at the end of the block with Pyjo.Server.Daemon.new(app=Pyjo.HelloWorld.new(), listen=['http://*:3000']) as daemon: daemon.run()
$ python examples/hello_world_winloop.py
Web application available at http://127.0.0.1:3000
$ curl http://127.0.0.1:3000/
Your Pyjo is working!
examples/asyncio_winloop.py is a coroutine on the loop of Winloop, which
requests the server of Pyjoyment in the same loop with the streams of
asyncio and with the user agent of Pyjoyment, as the libraries of
asyncio run along with the reactor:
import os # The reactor of Pyjoyment on Winloop, selected before Pyjo.IOLoop is loaded os.environ['PYJO_REACTOR'] = 'Pyjo.Reactor.Winloop' import Pyjo.HelloWorld # noqa: E402 import Pyjo.IOLoop # noqa: E402 import Pyjo.Server.Daemon # noqa: E402 import Pyjo.UserAgent # noqa: E402 import asyncio # noqa: E402 import winloop # noqa: E402 # A coroutine on the loop of Winloop, with the streams of asyncio and the user agent of Pyjoyment, both requesting the # server of Pyjoyment in the same loop async def main(port): reader, writer = await asyncio.open_connection('127.0.0.1', port) writer.write(b'GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n') print(f'asyncio: {(await reader.readline()).decode().strip()}') writer.close() await writer.wait_closed() with Pyjo.UserAgent.new() as ua: tx = await ua.get_p(f'http://127.0.0.1:{port}/') print(f'Pyjo.UserAgent: {tx.res.code} {tx.res.text}') return 'winloop' if isinstance(asyncio.get_running_loop(), winloop.Loop) else 'asyncio' # The server, on a port that the system picks, which closes its listen sockets at the end of the block with Pyjo.Server.Daemon.new(app=Pyjo.HelloWorld.new(), listen=['http://127.0.0.1'], silent=True) as daemon: port = daemon.start().ports()[0] Pyjo.IOLoop.singleton.reactor.run_coroutine( main(port), lambda loop: (print(f'loop: {loop}'), Pyjo.IOLoop.stop()), lambda err: (print(f'error: {err}'), Pyjo.IOLoop.stop()), ) Pyjo.IOLoop.start()
$ python examples/asyncio_winloop.py
asyncio: HTTP/1.1 200 OK
Pyjo.UserAgent: 200 Your Pyjo is working!
loop: winloop
examples/loopback_winloop.py is a server of Pyjoyment and its user agent on
the loop of Winloop, a blocking request, which runs the event loop until the
response, and the class of the reactor and whether its loop is one of
Winloop:
import os # The reactor of Pyjoyment on Winloop, selected before Pyjo.IOLoop is loaded os.environ['PYJO_REACTOR'] = 'Pyjo.Reactor.Winloop' import Pyjo.HelloWorld # noqa: E402 import Pyjo.IOLoop # noqa: E402 import Pyjo.Server.Daemon # noqa: E402 import Pyjo.UserAgent # noqa: E402 import winloop # noqa: E402 # The server, on a port that the system picks, and the user agent, in the same event loop, which close their sockets at # the end of the block with ( Pyjo.Server.Daemon.new(app=Pyjo.HelloWorld.new(), listen=['http://127.0.0.1'], silent=True) as daemon, Pyjo.UserAgent.new(ioloop=Pyjo.IOLoop.singleton) as ua, ): # A blocking request, which runs the event loop of the server on Winloop until the response tx = ua.get(f'http://127.0.0.1:{daemon.start().ports()[0]}/') reactor = Pyjo.IOLoop.singleton.reactor print(type(reactor).__name__, 'winloop' if isinstance(reactor.loop, winloop.Loop) else 'asyncio') print(tx.result.code, tx.result.text)
$ python examples/loopback_winloop.py
Pyjo_Reactor_Winloop winloop
200 Your Pyjo is working!