-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_example.py
More file actions
64 lines (47 loc) · 1.86 KB
/
sync_example.py
File metadata and controls
64 lines (47 loc) · 1.86 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
import os
import sys
import threading
import time
OUTPUT_FILE = "output.txt"
descriptors_for_read_from = []
def simulate_external_input(write_end_fd: int):
try:
for _ in range(10):
os.write(write_end_fd, "External message from pipe\n".encode())
time.sleep(1)
finally:
# Ensure the write-end of the pipe is closed
os.close(write_end_fd)
# If pipe is closed, we do not need to read from it
descriptors_for_read_from.pop(0)
def event_loop():
while True:
for read in descriptors_for_read_from:
# Blocking operation (!): waits until there is something to read
data_from_file_by_descriptor = os.read(read, 1024)
# If EOF is called from stdin, exit the loop
if not data_from_file_by_descriptor and read == sys.stdin.fileno():
print("EOF was called from stdin, exiting...")
return
# Write the read data to the output file
with open(OUTPUT_FILE, "a") as f:
f.write(data_from_file_by_descriptor.decode())
if __name__ == "__main__":
# Just some actions for example:
# 1. Pipe creating
# 2. Adding descriptors (stdin & pipe read-end) for reading in loop
# 3. Starting a thread to simulate external writing to the pipe
# 4. After finishing event_loop(), terminating thread, closing pipe's read-end
read_end_pipe_descriptor, write_end_pipe_descriptor = os.pipe()
descriptors_for_read_from.append(read_end_pipe_descriptor)
descriptors_for_read_from.append(sys.stdin.fileno())
external_input_thread = threading.Thread(
target=simulate_external_input,
args=(write_end_pipe_descriptor,),
)
external_input_thread.start()
try:
event_loop()
finally:
external_input_thread.join()
os.close(read_end_pipe_descriptor)