forked from EMSC-CSEM/hmb_client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisten_hmb.py
executable file
·165 lines (121 loc) · 5.12 KB
/
listen_hmb.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
import sys
import time
import getpass
import logging
from argparse import ArgumentParser
from multiprocessing import Queue, Process
from emschmb import EmscHmbListener, load_hmbcfg
# here you can import the function you want to launch
# BUT it has to be named 'process_message'
# for example
from my_processing import process_message
__version__ = '1.01'
def _process_wrapper(p, msg, tag):
try:
tick = time.time()
p(msg)
logging.info('%s ended in %.1f s', tag, time.time() - tick)
except Exception as e:
logging.exception("Unexpected exception during message processing: %s", str(e))
def shellprocess_manager_multithread(hmb, maxprocess=3):
process_queue = Queue()
hmbthread = Process(name='hmbthread', target=launch_hmb, args=(process_queue, hmb))
hmbthread.start()
local_pid = 1
running_processes = []
while True:
check_running_processes = [p for p in running_processes if p.is_alive() is True]
if len(check_running_processes) >= maxprocess:
logging.debug('- Queue full, loop : %s', running_processes)
time.sleep(1)
continue
msg = process_queue.get()
try:
tag = 'Process_{0}'.format(local_pid)
p = Process(name=tag, target=_process_wrapper, args=(process_message, msg, tag))
p.start()
local_pid += 1
logging.debug('- Launch shell process : %s -> %s', tag, p)
check_running_processes.append(p)
except Exception as e:
logging.exception('Unexpected exception : %s', str(e))
running_processes = check_running_processes
hmbthread.join()
def shellprocess_manager_singlethread(hmb):
process_queue = Queue()
hmbthread = Process(name='hmbthread', target=launch_hmb, args=(process_queue, hmb))
hmbthread.start()
while True:
msg = process_queue.get()
tick = time.time()
try:
process_message(msg)
logging.info('End process in %.1f s', time.time() - tick)
except Exception as e:
logging.exception('Unexpected exception : %s', str(e))
hmbthread.join()
def shellprocess_manager_nothread(hmb):
logging.debug('Begin hmb listener...')
hmb.listen(process_message)
def launch_hmb(pqueue, hmbsession):
def _process_closure(msg):
logging.info('- hmb msg: %s', msg.keys())
pqueue.put(msg)
logging.debug('Begin hmb listener...')
hmbsession.listen(_process_closure)
if __name__ == '__main__':
argd = ArgumentParser()
argd.add_argument('url', help='adresse of the hmb bserver')
argd.add_argument('--cfg', help='config file for connexion parameters (e.g. queue, user, password)')
argd.add_argument('--timeout', help='define timeout', type=int, default=30)
argd.add_argument('--nlast', help='n last message to get backNumber of messages to backfill from the server', type=int, default=10)
argd.add_argument('--queue', help='define the queue to listen')
argd.add_argument('--user', help='connexion authentication')
argd.add_argument('--password', help='connexion authentication')
argd.add_argument('--nthreads', help='number of concurrent running threads', type=int, default=3)
argd.add_argument('--singlethread', help='force single thread running (useful for debugging)', action='store_true')
argd.add_argument('--nothread', help='force no threading (useful for debugging)', action='store_true')
argd.add_argument('-v', '--verbose', action='store_true')
args = argd.parse_args()
dargs = vars(args)
logging.basicConfig(
stream=sys.stderr, level=logging.DEBUG if args.verbose else logging.INFO,
format='%(asctime)s:%(levelname)s:%(name)s:%(message)s')
logging.info('Listen HMB (%s)', __version__)
if args.cfg is not None:
cfg = load_hmbcfg(args.cfg)
else:
cfg = {}
for k in ['queue', 'user', 'password']:
if dargs[k] is not None:
cfg[k] = dargs[k]
cfgnopassword = cfg.copy()
if 'password' in cfg:
cfgnopassword['password'] = '****'
logging.info('Configs : %s', cfgnopassword)
url = args.url
if 'queue' not in cfg:
argd.error('queue parameter is mandatory in cmd or cfg')
queue = cfg['queue']
user = cfg.get('user')
password = cfg.get('password')
if user is not None and password is None:
password = getpass.getpass('Password for {0} : '.format(user))
heartbeat = args.timeout / 2
hmb = EmscHmbListener(url, heartbeat=heartbeat)
auth = None
if user is not None and password is not None:
logging.info('Use authentication')
hmb.authentication(user, password)
queue = queue.split(',')
hmb.queue(*queue, nlast=args.nlast)
if args.nothread:
logging.info('No thread processing')
shellprocess_manager_nothread(hmb)
elif args.singlethread:
logging.info('Single thread processing')
shellprocess_manager_singlethread(hmb)
else:
logging.info('Multi threads processing (%d process(es))', args.nthreads)
shellprocess_manager_multithread(hmb, maxprocess=args.nthreads)