Skip to content

Commit c16eccd

Browse files
committed
OR updates
1 parent 740c24c commit c16eccd

File tree

5 files changed

+90
-76
lines changed

5 files changed

+90
-76
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import subprocess
2+
3+
4+
def get_char(process):
5+
character = process.stdout.read(1)
6+
print(
7+
character,
8+
end="",
9+
flush=True, # So display is the same as running it directly
10+
)
11+
return character
12+
13+
14+
def search_for_output(string, process):
15+
buffer = ""
16+
while True:
17+
buffer = buffer + get_char(process)
18+
if string in buffer:
19+
return
20+
21+
22+
with subprocess.Popen(
23+
[
24+
"python",
25+
"-u", # So display is the same as running it directly
26+
"choice_react.py",
27+
],
28+
stdin=subprocess.PIPE,
29+
stdout=subprocess.PIPE,
30+
text=True,
31+
) as process:
32+
33+
buffer = ""
34+
35+
search_for_output("when you are ready\n", process)
36+
process.stdin.write("\n")
37+
process.stdin.flush()
38+
search_for_output("==\n= ", process)
39+
stdout, stderr = process.communicate(f"{get_char(process)}\n", timeout=10)
40+
print(stdout)
41+
42+
43+
print(process)

subprocess/exiter_run.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import subprocess
2+
3+
subprocess.run(["python", "exiter.py"], check=False)
4+
5+
try:
6+
subprocess.run(["python", "exiter.py"], check=True)
7+
except subprocess.CalledProcessError as exc:
8+
print(exc)
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import subprocess
22

3-
print(subprocess.check_output(["python", "random_num_gen.py"]).strip())
3+
process = subprocess.run(
4+
["python", "random_num_gen.py"], capture_output=True, text=True
5+
)
6+
7+
print(int(process.stdout))

subprocess/simple_react_popen_read_write.py

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
import subprocess
22
from time import sleep
33

4-
process = subprocess.Popen("python timer.py 5")
5-
6-
print(process.poll()) # returns None because process not yet finished
7-
sleep(3)
8-
print(process.poll()) # None
9-
sleep(3)
10-
print(process.poll()) # 0
11-
12-
process = subprocess.Popen("python timer.py 5", stdout=subprocess.PIPE)
13-
14-
print(process.poll())
15-
print(process.stdout.read()) # reads everything and blocks until EOF
16-
sleep(3)
17-
print(process.poll()) # returns 0 because that is exit code
18-
print(process.stdout.read()) # b''
19-
sleep(3)
20-
print(process.poll()) # 0
21-
print(process.stdout.read()) # b''
22-
23-
24-
process = subprocess.Popen("python timer.py 5", stdout=subprocess.PIPE)
25-
26-
# https://stackoverflow.com/questions/57726771/what-the-difference-between-read-and-read1-in-python
27-
# https://docs.python.org/3/library/io.html?highlight=read1#io.BufferedIOBase.read1
28-
# https://docs.python.org/3/library/io.html?highlight=read1#io.BufferedIOBase.read
29-
30-
print(process.poll())
31-
print(process.stdout.read1())
32-
sleep(3)
33-
print(process.poll())
34-
print(process.stdout.read1())
35-
sleep(3)
36-
print(process.poll())
37-
print(process.stdout.read1())
4+
with subprocess.Popen("python timer.py 5") as process:
5+
6+
print(process.poll()) # returns None because process not yet finished
7+
sleep(3)
8+
print(process.poll()) # None
9+
sleep(3)
10+
print(process.poll()) # 0
11+
12+
with subprocess.Popen("python timer.py 5", stdout=subprocess.PIPE) as process:
13+
14+
print(process.poll())
15+
print(process.stdout.read()) # reads everything and blocks until EOF
16+
sleep(3)
17+
print(process.poll()) # returns 0 because that is exit code
18+
print(process.stdout.read()) # b''
19+
sleep(3)
20+
print(process.poll()) # 0
21+
print(process.stdout.read()) # b''
22+
23+
24+
with subprocess.Popen("python timer.py 5", stdout=subprocess.PIPE) as process:
25+
26+
# https://stackoverflow.com/questions/57726771/what-the-difference-between-read-and-read1-in-python
27+
# https://docs.python.org/3/library/io.html?highlight=read1#io.BufferedIOBase.read1
28+
# https://docs.python.org/3/library/io.html?highlight=read1#io.BufferedIOBase.read
29+
30+
print(process.poll())
31+
print(process.stdout.read1())
32+
sleep(3)
33+
print(process.poll())
34+
print(process.stdout.read1())
35+
sleep(3)
36+
print(process.poll())
37+
print(process.stdout.read1())

0 commit comments

Comments
 (0)