-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_demo.py
More file actions
70 lines (56 loc) · 2.03 KB
/
Copy pathshell_demo.py
File metadata and controls
70 lines (56 loc) · 2.03 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Demo showing a shell session running alongside other cooperative tasks."""
from common import build_runtime
from SmallPackage import SmallTask, Unix
from SmallPackage.shells import BaseShell
async def background_worker(task):
"""Keep producing app output while the shell inspects the runtime."""
for step in range(12):
task.OS.print("[{}] working step {}\n".format(task.name, step))
await task.sleep(0.05)
return task.name
def _pid_for_name(os_ref, name):
"""Look up a live task PID by task name for shell scripting."""
for item in os_ref.tasks.list():
if item.name == name:
return item.getID()
return None
async def shell_session(task):
"""
Drive a scripted shell session while other tasks continue to run.
This avoids a blocking stdin loop and makes the demo portable. The shell
still uses the same command parser and runtime APIs as an interactive shell
would.
"""
shell = task.OS.shells[0]
worker_pid = _pid_for_name(task.OS, "background_worker")
script = [
"toggle",
"help",
"count",
"ps",
"signal {} 3".format(worker_pid) if worker_pid is not None else "ps",
"signals {}".format(worker_pid) if worker_pid is not None else "ps",
"io status",
"stat {}".format(worker_pid) if worker_pid is not None else "ps",
"toggle",
]
for command in script:
await task.sleep(0.05)
shell.run(command, show_prompt=False, echo_command=True, force_output=True)
if not shell.is_running:
break
return "shell session complete"
def main():
shell = BaseShell()
runtime = build_runtime(Unix())
runtime.shells.append(shell.setOS(runtime))
runtime.fork(
[
SmallTask(2, shell_session, name="shell_session"),
SmallTask(4, background_worker, name="background_worker"),
SmallTask(6, background_worker, name="background_worker_low"),
]
)
runtime.startOS()
if __name__ == "__main__":
main()