diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-02-26 04:10:02 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-02-26 04:10:02 +0000 |
commit | 227038a454943a075c51b60988c53f77a605fa10 (patch) | |
tree | 15d438e5519a2ba9ee7ba8c6f864bbc597ea33b5 /examples/asyncio-python-embed.py | |
parent | Initial commit. (diff) | |
download | ptpython-227038a454943a075c51b60988c53f77a605fa10.tar.xz ptpython-227038a454943a075c51b60988c53f77a605fa10.zip |
Adding upstream version 3.0.16.upstream/3.0.16
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/asyncio-python-embed.py')
-rwxr-xr-x | examples/asyncio-python-embed.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/examples/asyncio-python-embed.py b/examples/asyncio-python-embed.py new file mode 100755 index 0000000..05f52f1 --- /dev/null +++ b/examples/asyncio-python-embed.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +""" +(Python >3.3) + +This is an example of how we can embed a Python REPL into an asyncio +application. In this example, we have one coroutine that runs in the +background, prints some output and alters a global state. The REPL, which runs +inside another coroutine can access and change this global state, interacting +with the running asyncio application. +The ``patch_stdout`` option makes sure that when another coroutine is writing +to stdout, it won't break the input line, but instead writes nicely above the +prompt. +""" +import asyncio + +from ptpython.repl import embed + +loop = asyncio.get_event_loop() +counter = [0] + + +async def print_counter(): + """ + Coroutine that prints counters and saves it in a global variable. + """ + while True: + print("Counter: %i" % counter[0]) + counter[0] += 1 + await asyncio.sleep(3) + + +async def interactive_shell(): + """ + Coroutine that starts a Python REPL from which we can access the global + counter variable. + """ + print( + 'You should be able to read and update the "counter[0]" variable from this shell.' + ) + try: + await embed(globals=globals(), return_asyncio_coroutine=True, patch_stdout=True) + except EOFError: + # Stop the loop when quitting the repl. (Ctrl-D press.) + loop.stop() + + +def main(): + asyncio.ensure_future(print_counter()) + asyncio.ensure_future(interactive_shell()) + + loop.run_forever() + loop.close() + + +if __name__ == "__main__": + main() |