Last change
on this file since 963 was
962,
checked in by djay, 4 years ago
|
Update OGC API - Processes documentation and implementation, providing a browsable User Interface to Processes.
|
-
Property svn:executable set to
*
|
File size:
1.5 KB
|
Rev | Line | |
---|
[962] | 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 | |
---|
| 14 | mThreads=[] |
---|
| 15 | r = redis.Redis(host='localhost', port=6379, db=0) |
---|
| 16 | |
---|
| 17 | def send(t): |
---|
| 18 | # send string to web page |
---|
| 19 | stdout.write(t+'\n') |
---|
| 20 | stdout.flush() |
---|
| 21 | |
---|
| 22 | def listenMessages(jobID=None): |
---|
| 23 | global r |
---|
| 24 | p = r.pubsub() |
---|
| 25 | p.subscribe(jobID) |
---|
| 26 | hasSend=False |
---|
| 27 | for raw_message in p.listen(): |
---|
| 28 | try: |
---|
| 29 | send(str(raw_message["data"],'utf-8')) |
---|
| 30 | hasSend=True |
---|
| 31 | try: |
---|
| 32 | tmp=json.loads(str(raw_message["data"],'utf-8')) |
---|
| 33 | if tmp is not None and "outputs" in tmp: |
---|
| 34 | sys.exit() |
---|
| 35 | except Exception as e: |
---|
| 36 | print(str(e)) |
---|
| 37 | return |
---|
| 38 | except: |
---|
| 39 | if not(hasSend): |
---|
| 40 | send(str(raw_message["data"])) |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | def receive(): |
---|
| 44 | global n |
---|
| 45 | global mThreads |
---|
| 46 | while True: |
---|
| 47 | t = stdin.readline().strip() |
---|
| 48 | if not t: |
---|
| 49 | break |
---|
| 50 | t1 = t.split(" ") |
---|
| 51 | if t1[0]=="SUB": |
---|
| 52 | mThreads += [threading.Thread(target=listenMessages,kwargs={"jobID":t1[1]})] |
---|
| 53 | mThreads[len(mThreads)-1].start() |
---|
| 54 | else: |
---|
| 55 | send(t) |
---|
| 56 | |
---|
| 57 | t0 = threading.Thread(target=receive) |
---|
| 58 | t0.start() |
---|
| 59 | |
---|
| 60 | t0.join() |
---|
| 61 | for i in range(len(mThreads)): |
---|
| 62 | mThreads[i].join() |
---|
Note: See
TracBrowser
for help on using the repository browser.