-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathffmpeg_screenshot_pipe_multi.py
115 lines (96 loc) · 3.08 KB
/
ffmpeg_screenshot_pipe_multi.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
from multiprocessing import Process, SimpleQueue, Value
import asyncio
from kthread_sleep import sleep
from collections import defaultdict, deque
from ffmpeg_screenshot_pipe import start_multiprocessing
procresults = sys.modules[__name__]
procresults.processes = []
procresults.allthreads = []
procresults.dequebuffer = 24
lambdadeq = lambda: deque([], procresults.dequebuffer)
procresults.results = defaultdict(lambdadeq)
procresults.result_queue = SimpleQueue()
procresults.tasks = 2
procresults.function2execute = start_multiprocessing
procresults.asyncsleep=0.001
procresults.stop_flag = Value('b', False)
procresults.stop = None
procresults.stoploop=None
procresults.sleeptimeafterkill = 5
procresults.sleeptimebeforekill = lambda : procresults.sleeptimeafterkill * .9
def stopfunction():
try:
try:
procresults.stop_flag.value = True
sleep(procresults.sleeptimeafterkill)
except Exception:
pass
try:
while True:
procresults.result_queue.close()
break
except Exception:
pass
try:
procresults.stoploop.stop()
except Exception:
pass
try:
procresults.stoploop.close()
except Exception:
pass
for pa in procresults.processes:
try:
if pa.is_alive():
pa.terminate()
except Exception:
continue
except Exception:
pass
def runasync(args):
procresults.tasks =len(args)
if procresults.tasks == 1:
procresults.asyncsleep = 0
async def process_output():
while True:
try:
if procresults.stop_flag.value:
break
chunk = procresults.result_queue.get()
procresults.results[chunk[0]].append(chunk[-1])
await asyncio.sleep(procresults.asyncsleep) # Allow other tasks to run
except Exception:
continue
def fx():
sleeptimebeforekilling = procresults.sleeptimebeforekill()
for i,mydi in zip(range(procresults.tasks),args):
procresults.processes.append(
Process(target=procresults.function2execute, args=(sleeptimebeforekilling,i, procresults.stop_flag, procresults.result_queue),
kwargs=mydi))
procresults.processes[-1].daemon = True
procresults.processes[-1].start()
async def main():
await asyncio.gather(*[process_output() for _ in range(procresults.tasks)])
fx()
loop = asyncio.get_event_loop()
procresults.stoploop = loop
procresults.stop=lambda:stopfunction()
try:
loop.run_until_complete(main())
except Exception:
pass
finally:
try:
try:
loop.stop()
except Exception:
pass
try:
loop.close()
except Exception:
pass
except Exception as fe:
print(fe)
if __name__ =='__main__':
pass