Pyjo.Steps - Steps of a coroutine, blocking or non-blocking¶
from Pyjo.Steps import Command, Request, Sleep, Steps
from Pyjo.UserAgent import UserAgent
ua = UserAgent()
async def hello():
tx = await Request(ua.build_tx('GET', 'https://mojolicious.org'))
await Sleep(0.5)
code, out, err = await Command(['hostname'])
return f"{out.decode().strip()}: {tx.result.dom().at('title').text}"
print(Steps(hello()).run(ua))
Steps(hello()).run_p(ua).then(lambda text: print(text)).wait()
# Or import the module, as in Google Python Style Guide
import Pyjo.Steps
# The steps of another coroutine, awaited
async def twice():
return [await Pyjo.Steps.Steps(hello()), await Pyjo.Steps.Steps(hello())]
Pyjo.Steps runs a coroutine that awaits transactions of HTTP,
programs and sleeps, blocking and non-blocking, so that the code that makes
requests and runs programs is written once, for both, as a library that a
script and an action of a server share:
a
Requestgives the transaction back with its response, or raises thePyjo.Exceptionof an error of the connection;a
Commandgives the exit code, the output and the error of the program, or raises thePyjo.Exceptionof a program that cannot be run;a
Sleepwaits for a number of seconds;Stepsof another coroutine give what it returns.
A blocking request of Pyjo.UserAgent cannot run the event loop that
is running already, as in an action of a server, and a non-blocking one
cannot be waited for there either. The same steps run with
run() in a script and with run_p() in
the event loop.
No event loop runs the coroutine, so it awaits nothing else, not a
Pyjo.Promise and not a future of asyncio, which is a
TypeError. A generator that yields the requests, the programs and the
sleeps, and takes Steps with yield from, runs as a coroutine
does.
See also¶
Classes¶
- class Pyjo.Steps.Command(args: Sequence[str], input: bytes = b'', *, env: Mapping[str, str] | None = None, timeout: float | None = None)[source]¶
A program to run, with its arguments, what it gets on its standard input, its environment, the one of the process by default, and the number of seconds that it may run, without a limit by default.
code, out, err = await Command.new(['hostname']) code, out, err = await Command.new(['/opt/bin/token'], env={**os.environ, 'AUDIENCE': audience}, timeout=30)
- class Pyjo.Steps.Pyjo_Steps(steps: Coroutine[Any, Any, T] | Generator[Operation, Any, T])[source]¶
Pyjo.Stepsinherits all attributes and methods fromPyjo.Baseand implements the following new ones. The steps are of one coroutine, which they run once.- __await__() Generator[Operation, Any, T][source]¶
value = await steps
Wait for the steps in another coroutine and give what they return.
- __iter__() Generator[Operation, Any, T][source]¶
value = yield from steps
Same as
__await__(), in a generator.
- run(ua: Pyjo_UserAgent) T[source]¶
value = steps.run(ua)
Run the steps blocking, the requests with a user agent, of
Pyjo.UserAgent.Pyjo_UserAgent.start(), the programs ofsubprocess.run()and the sleeps oftime.sleep(), and give what they return.
- run_p(ua: Pyjo_UserAgent) Pyjo_Promise[source]¶
promise = steps.run_p(ua)
Same as
run(), but non-blocking, withPyjo.UserAgent.Pyjo_UserAgent.start_p(),Pyjo.IOLoop.ChildProcess.Pyjo_IOLoop_ChildProcess.run_p()andPyjo.Promise.Pyjo_Promise.timer(), and returns aPyjo.Promiseobject.
- class Pyjo.Steps.Request(tx: Pyjo_Transaction_HTTP, ua: Pyjo_UserAgent | None = None)[source]¶
A transaction to start, with the user agent that starts it, or the one of
run()by default, as one whose eventstartadds the credentials of a client to it.tx = await Request.new(ua.build_tx('GET', 'https://mojolicious.org')) tx = await Request.new(tx, ua)
- __await__() Generator[Self, Any, Pyjo_Transaction_HTTP][source]¶
tx = await request
Start the transaction and give it back with its response.