Pyjo.IOLoop.Stream - Non-blocking I/O stream¶
from Pyjo.IOLoop.Stream import Stream
# Create stream
stream = Stream(handle)
stream.on('read', lambda stream, chunk: ...)
stream.on('close', lambda stream: ...)
stream.on('error', lambda stream, err: ...)
# Or import the module, as in Google Python Style Guide
import Pyjo.IOLoop.Stream
# Create stream
stream = Pyjo.IOLoop.Stream.new(handle)
stream.on('read', lambda stream, chunk: ...)
stream.on('close', lambda stream: ...)
stream.on('error', lambda stream, err: ...)
# Start and stop watching for new data
stream.start()
stream.stop()
# Start reactor if necessary
if not stream.reactor.is_running:
stream.reactor.start()
Pyjo.IOLoop.Stream is a container for I/O streams used by
Pyjo.IOLoop. A handle is a socket, an ssl.SSLSocket too, or
anything else with a file descriptor, like a pipe.
The end of a with block closes the stream.
Events¶
Pyjo.IOLoop.Stream inherits all events from Pyjo.EventEmitter
and can emit the following new ones.
close¶
stream.on('close', lambda stream: ...)
Emitted if the stream gets closed.
drain¶
stream.on('drain', lambda stream: ...)
Emitted once all data has been written.
error¶
stream.on('error', lambda stream, err: ...)
Emitted if an error occurs on the stream, fatal if unhandled. The error is
the OSError of the handle.
read¶
stream.on('read', lambda stream, chunk: ...)
Emitted if new data arrives on the stream.
timeout¶
stream.on('timeout', lambda stream: ...)
Emitted if the stream has been inactive for too long and will get closed automatically.
write¶
stream.on('write', lambda stream, chunk: ...)
Emitted if new data has been written to the stream.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojo::IOLoop::Stream of Mojolicious 9.49.
- class Pyjo.IOLoop.Stream.Pyjo_IOLoop_Stream(handle: Any = None)[source]¶
Pyjo.IOLoop.Streaminherits all methods fromPyjo.EventEmitterand implements the following new ones.- property bytes_waiting: int[source]¶
num = stream.bytes_waiting
Number of bytes that have been enqueued with
write()and are waiting to be written.
- property handle: Any[source]¶
handle = stream.handle
Get handle for stream, usually a
socket.socketorssl.SSLSocketobject.
- high_water_mark¶
size = stream.high_water_mark stream.high_water_mark = 1024
Maximum size of
write()buffer in bytes beforecan_writeis false, defaults to1048576(1MiB).
- property is_readable: bool[source]¶
boolean = stream.is_readable
Quick non-blocking check if stream is readable, useful for identifying tainted sockets.
- property reactor: Pyjo_Reactor[source]¶
reactor = stream.reactor stream.reactor = Pyjo.Reactor.Poll.new()
Low-level event reactor, defaults to the
reactorattribute value of the globalPyjo.IOLoopsingleton. Note that this attribute is weakened.
- steal_handle() Any[source]¶
handle = stream.steal_handle()
Steal
handleand prevent it from getting closed automatically.