This repository was archived by the owner on Nov 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
58 lines (45 loc) · 1.58 KB
/
process.py
File metadata and controls
58 lines (45 loc) · 1.58 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
# coding: utf-8
import subprocess
import typing
import time
import _thread as thread
class ProcessesController:
def __init__(self):
self.processes = []
def create_process(self, process_name, *args, **kwargs):
kwargs['stdin'] = subprocess.PIPE
kwargs['stdout'] = subprocess.PIPE
kwargs['stderr'] = subprocess.PIPE
self.processes.append(
[process_name, subprocess.Popen(*args, **kwargs), b""])
thread.start_new_thread(
self.__class__.subthread_read_stdout, (self, len(self.processes) - 1))
thread.start_new_thread(
self.__class__.subthread_read_stderr, (self, len(self.processes) - 1))
return len(self.processes) - 1
def subthread_read_stdout(self, pid):
time.sleep(0.1)
try:
while True:
self.processes[pid][2] += self.processes[pid].stdout.read()
except:
return
def subthread_read_stderr(self, pid):
time.sleep(0.1)
try:
while True:
self.processes[pid][2] += self.processes[pid].stderr.read()
except:
return
def get_processes(self):
return self.processes
def kill_process(self, process_id):
return self.processes[process_id][1].kill()
def read_stdout(self, process_id):
now = self.processes[process_id][2]
self.processes[process_id][2] = b''
return now
def read_flush(self, process_id):
return b""
def put_stdin(self, process_id, s):
return self.processes[process_id][1].stdin.write(s)