-
Notifications
You must be signed in to change notification settings - Fork 39
/
16_subprocess.sql
62 lines (43 loc) · 1.71 KB
/
16_subprocess.sql
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
subprocess.run("echo 'Hello World'", shell=True)
output = subprocess.run("echo 'Hello World'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
print(output)
print(output.stdout)
output = subprocess.run("echo Hello World", shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
print(output)
output = subprocess.run("echo 'Hello World", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
print(output)
from subprocess import Popen
p = Popen(["sleep","5"])
#p.wait()
from subprocess import Popen,PIPE
p1 = Popen(["ls","-1"], stdout=PIPE)
p2 = Popen(["grep", "subprocess"], stdin=p1.stdout, stdout=PIPE, universal_newlines=True)
output = p2.stdout.readline()
print(output)
p1.stdout.close()
output, error = p2.communicate()
print(output)
###
process = subprocess.Popen(['ping', '-c 4', 'python.org'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
while True:
output = process.stdout.readline()
print(output)
return_code = process.poll()
if return_code is not None:
print('RETURN CODE', return_code)
for output in process.stdout.readlines():
print(output)
break
proc = subprocess.Popen(['ping', '-c 5', 'google.com'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
try:
outs, _ = proc.communicate(timeout=10)
print('RETURN CODE', proc.returncode)
print(outs.decode('utf-8'))
except subprocess.TimeoutExpired:
print('subprocess did not terminate in time')
proc.terminate()