Last change
on this file since 976 was
968,
checked in by djay, 4 years ago
|
Add websocketd container to docker-compose and make the OGC API - Processes and it basic UI available.
|
-
Property svn:executable set to
*
|
File size:
1.6 KB
|
Line | |
---|
1 | #!/usr/bin/python3 |
---|
2 | # cf. https://github.com/joewalnes/websocketd/wiki/Simple-Python-Duplex-Example |
---|
3 | # |
---|
4 | # example usage: |
---|
5 | # websocketd --port=4430 --ssl --sslcert /ssl/fullchain.pem --sslkey /ssl/privkey.pem subscriber.py --devconsole |
---|
6 | # |
---|
7 | |
---|
8 | from sys import stdout, stdin |
---|
9 | import sys |
---|
10 | import threading |
---|
11 | import redis |
---|
12 | import json |
---|
13 | import os |
---|
14 | |
---|
15 | mThreads=[] |
---|
16 | r=None |
---|
17 | |
---|
18 | if "ZOO_REDIS_HOST" in os.environ: |
---|
19 | r = redis.Redis(host=os.environ["ZOO_REDIS_HOST"], port=6379, db=0) |
---|
20 | else: |
---|
21 | r = redis.Redis(host='redis', port=6379, db=0) |
---|
22 | |
---|
23 | def send(t): |
---|
24 | # send string to web page |
---|
25 | stdout.write(t+'\n') |
---|
26 | stdout.flush() |
---|
27 | |
---|
28 | def listenMessages(jobID=None): |
---|
29 | global r |
---|
30 | p = r.pubsub() |
---|
31 | p.subscribe(jobID) |
---|
32 | hasSend=False |
---|
33 | for raw_message in p.listen(): |
---|
34 | try: |
---|
35 | send(str(raw_message["data"],'utf-8')) |
---|
36 | hasSend=True |
---|
37 | try: |
---|
38 | tmp=json.loads(str(raw_message["data"],'utf-8')) |
---|
39 | if tmp is not None and "outputs" in tmp: |
---|
40 | sys.exit() |
---|
41 | except Exception as e: |
---|
42 | print(str(e)) |
---|
43 | return |
---|
44 | except: |
---|
45 | if not(hasSend): |
---|
46 | send(str(raw_message["data"])) |
---|
47 | |
---|
48 | |
---|
49 | def receive(): |
---|
50 | global n |
---|
51 | global mThreads |
---|
52 | while True: |
---|
53 | t = stdin.readline().strip() |
---|
54 | if not t: |
---|
55 | break |
---|
56 | t1 = t.split(" ") |
---|
57 | if t1[0]=="SUB": |
---|
58 | mThreads += [threading.Thread(target=listenMessages,kwargs={"jobID":t1[1]})] |
---|
59 | mThreads[len(mThreads)-1].start() |
---|
60 | else: |
---|
61 | send(t) |
---|
62 | |
---|
63 | t0 = threading.Thread(target=receive) |
---|
64 | t0.start() |
---|
65 | |
---|
66 | t0.join() |
---|
67 | #for i in range(len(mThreads)): |
---|
68 | # mThreads[i].join() |
---|
Note: See
TracBrowser
for help on using the repository browser.