forked from qiniu/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox_commands.py
More file actions
62 lines (54 loc) · 1.95 KB
/
Copy pathsandbox_commands.py
File metadata and controls
62 lines (54 loc) · 1.95 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
# -*- coding: utf-8 -*-
from __future__ import print_function
from qiniu.services.sandbox import SandboxError
from sandbox_common import cleanup_sandbox, create_sandbox, run_example
def main():
sandbox = create_sandbox(metadata={'example': 'sandbox_commands'})
try:
stdout = []
result = sandbox.commands.run(
'printf command-callback',
on_stdout=stdout.append,
)
print('run:', result.stdout)
print('callback:', ''.join(stdout))
handle = sandbox.commands.start(
'read line; echo "stdin:$line"',
stdin=True,
tag='qiniu-python-sdk-commands',
timeout=30,
)
print('started pid:', handle.pid)
print('processes:', [
item.pid for item in sandbox.commands.list()
if item.pid == handle.pid or item.tag == 'qiniu-python-sdk-commands'
])
try:
sandbox.commands.send_stdin(handle.pid, 'hello\n')
sandbox.commands.close_stdin(handle.pid)
print('stdin result:', handle.wait().stdout.strip())
except SandboxError as err:
print('stdin skipped:', err)
try:
sandbox.commands.kill(handle.pid)
except SandboxError:
pass
sleeper = sandbox.commands.run(
'sleep 30',
background=True,
tag='qiniu-python-sdk-kill',
)
print('background pid:', sleeper.pid)
try:
connected = sandbox.commands.connect(sleeper.pid)
print('connect running pid:', connected.pid)
except SandboxError as err:
print('connect running process skipped:', err)
try:
print('kill background:', sandbox.commands.kill(sleeper.pid))
except SandboxError as err:
print('kill background skipped:', err)
finally:
cleanup_sandbox(sandbox)
if __name__ == '__main__':
run_example(main)