-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_multithread.py
46 lines (38 loc) · 1.37 KB
/
example_multithread.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
import argparse
import collections
import time
import threading
from py_elasticinfra.utils.parse_config import ConfigParser
from py_elasticinfra.elk.elastic import Indexer
from py_elasticinfra.runner import Runner
def foreground_thread():
for i in range(5):
time.sleep(3)
print('[INFO] Foreground thread, iteration {}'.format(i+1))
def main(config):
# connect to elasticsearch
es = Indexer(config)
es.connect()
es.create_index()
# initialize threads and run in parallel
runner = Runner(config, es)
runner.run_background()
thread_main = threading.Thread(name="foreground_thread",
target=foreground_thread)
thread_main.start()
time.sleep(5)
runner.stop_background()
if __name__ == "__main__":
args = argparse.ArgumentParser(description="py_elasticinfra")
args.add_argument("-c", "--config", default=None, type=str,
help="config file path (default: None)")
# custom cli options to modify configuration
# from default values given in json file.
custom_args = collections.namedtuple("custom_args", "flags type target")
options = [
custom_args(["--elk", "--elk_host"], type=str,
target=("elk", "host"))
]
config = ConfigParser(parse_args=True, args=args, options=options)
config.init_logger()
main(config)