-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
executable file
·42 lines (35 loc) · 1.18 KB
/
main.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
import logging
import multiprocessing
import sys
class StreamToLogger(object):
def __init__(self, logger, log_level=logging.INFO):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
def write(self, buf):
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())
def flush(self):
pass
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
filename="threading.log",
filemode='a'
)
class JobProcess(multiprocessing.Process):
def __init__(self, name, job):
super(JobProcess, self).__init__()
self.name = name
self.job = job
def run(self):
thread_logger = logging.getLogger(self.name)
sys.stdout = StreamToLogger(thread_logger, logging.INFO)
sys.stderr = StreamToLogger(thread_logger, logging.ERROR)
thread_logger.info("Starting " + self.name + "...")
self.job.main()
print("Notice how regular print statements don't go to the log.")
testing_module = __import__("test_thread")
test_job = JobProcess("Thread 1", testing_module)
test_job.start()
print("Process queued!")