source: branches/branch-1.8/zoo-project/zoo-services/utils/open-api/server/subscriber.py @ 976

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
8from sys import stdout, stdin
9import sys
10import threading
11import redis
12import json
13import os
14
15mThreads=[]
16r=None
17
18if "ZOO_REDIS_HOST" in os.environ:
19    r = redis.Redis(host=os.environ["ZOO_REDIS_HOST"], port=6379, db=0)
20else:
21    r = redis.Redis(host='redis', port=6379, db=0)
22
23def send(t):
24    # send string to web page
25    stdout.write(t+'\n')
26    stdout.flush()
27
28def 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
49def 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
63t0 = threading.Thread(target=receive)
64t0.start()
65
66t0.join()
67#for i in range(len(mThreads)):
68#    mThreads[i].join()
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png