forked from prompt-toolkit/ptpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncio-python-embed.py
More file actions
executable file
·56 lines (43 loc) · 1.49 KB
/
asyncio-python-embed.py
File metadata and controls
executable file
·56 lines (43 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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.
"""
from __future__ import unicode_literals
from ptpython.repl import embed
import asyncio
loop = asyncio.get_event_loop()
counter = [0]
@asyncio.coroutine
def print_counter():
"""
Coroutine that prints counters and saves it in a global variable.
"""
while True:
print('Counter: %i' % counter[0])
counter[0] += 1
yield from asyncio.sleep(3)
@asyncio.coroutine
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.')
yield from embed(globals=globals(), return_asyncio_coroutine=True, patch_stdout=True)
# 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()