-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdocker_thread.py
More file actions
38 lines (32 loc) · 1017 Bytes
/
docker_thread.py
File metadata and controls
38 lines (32 loc) · 1017 Bytes
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
"""Docker Thread Class"""
import threading
import subprocess
import os
import signal
import sys
class DockerThread(threading.Thread):
"""Threaded Docker Thread Class"""
def __init__(self, cmd, shell=True):
threading.Thread.__init__(self)
self.cmd = cmd
self.process = None
self.shell = shell
def run(self):
self.process = subprocess.Popen(
self.cmd,
shell=self.shell,
stdout=sys.stdout,
stderr=subprocess.PIPE,
start_new_session=True,
bufsize=1024,
universal_newlines=True,
executable="/bin/bash",
)
self.process.communicate()
def terminate(self):
"""Terminates the thread and the process"""
if self.process:
try:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
except ProcessLookupError as error:
print(f"{self.process.pid}: Process already terminated {error}")