[959] | 1 | import argparse |
---|
| 2 | import requests |
---|
| 3 | import time |
---|
| 4 | |
---|
| 5 | from threading import Thread |
---|
| 6 | from xml.etree import ElementTree |
---|
| 7 | |
---|
| 8 | parser = argparse.ArgumentParser() |
---|
| 9 | parser.add_argument("threads", help="number of threads", type=int) |
---|
| 10 | args = parser.parse_args() |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | class LongProcess(Thread): |
---|
| 14 | def __init__(self, name): |
---|
| 15 | Thread.__init__(self) |
---|
| 16 | self.name = name |
---|
| 17 | self.progress = None |
---|
| 18 | self.location_url = self.launch_long_process() |
---|
| 19 | print("INIT %s %s" % (self.name, self.location_url)) |
---|
| 20 | |
---|
| 21 | def launch_long_process(self): |
---|
| 22 | endpoint_url = "http://localhost/cgi-bin/zoo_loader.cgi" |
---|
| 23 | params = dict( |
---|
| 24 | request="Execute", |
---|
| 25 | service="WPS", |
---|
| 26 | version="1.0.0", |
---|
| 27 | Identifier="longProcess", |
---|
| 28 | DataInputs="a=toto", |
---|
| 29 | ResponseDocument="Result", |
---|
| 30 | storeExecuteResponse="true", |
---|
| 31 | status="true", |
---|
| 32 | ) |
---|
| 33 | r = requests.get(endpoint_url, params=params) |
---|
| 34 | root = ElementTree.fromstring(r.content) |
---|
| 35 | |
---|
| 36 | location_url = root.attrib["statusLocation"] |
---|
| 37 | return location_url |
---|
| 38 | |
---|
| 39 | def get_progress(self): |
---|
| 40 | progress = None |
---|
| 41 | r = requests.get(self.location_url) |
---|
| 42 | root = ElementTree.fromstring(r.content) |
---|
| 43 | ns = dict(wps="http://www.opengis.net/wps/1.0.0") |
---|
| 44 | status = root.find("wps:Status", ns) |
---|
| 45 | if status: |
---|
| 46 | ctime = status.attrib["creationTime"] |
---|
| 47 | started = root.find("wps:Status/wps:ProcessStarted", ns) |
---|
| 48 | #started = root.find("wps:ProcessStarted", ns) |
---|
| 49 | succeeded = root.find("wps:Status/wps:ProcessSucceeded", ns) |
---|
| 50 | #succeeded = root.find("wps:ProcessSucceeded", ns) |
---|
| 51 | if started is not None and started.attrib["percentCompleted"]: |
---|
| 52 | percent = started.attrib["percentCompleted"] |
---|
| 53 | progress = int(percent) |
---|
| 54 | elif succeeded is not None: |
---|
| 55 | progress = 100 |
---|
| 56 | else: |
---|
| 57 | # error? |
---|
| 58 | print(r.content.decode()) |
---|
| 59 | progress = None |
---|
| 60 | else: |
---|
| 61 | print(r.content.decode()) |
---|
| 62 | progress = -1 |
---|
| 63 | |
---|
| 64 | self.progress = progress |
---|
| 65 | return self.progress |
---|
| 66 | |
---|
| 67 | def run(self): |
---|
| 68 | print("Thread %s: starting" % self.name) |
---|
| 69 | while True: |
---|
| 70 | progress = self.get_progress() |
---|
| 71 | if progress == 100: |
---|
| 72 | print("Thread %s: succeeded" % self.name) |
---|
| 73 | break |
---|
| 74 | elif progress == -1: |
---|
| 75 | print("Thread %s: failed" % self.name) |
---|
| 76 | break |
---|
| 77 | elif progress is None: |
---|
| 78 | print("Thread %s: error" % self.name) |
---|
| 79 | break |
---|
| 80 | else: |
---|
| 81 | print("Thread %s :progress %s" % (self.name, progress)) |
---|
| 82 | time.sleep(1.0) |
---|
| 83 | print("Thread %s: finishing" % self.name) |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | threads = list() |
---|
| 87 | for i in range(0, args.threads): |
---|
| 88 | t = LongProcess(i) |
---|
| 89 | t.start() |
---|
| 90 | threads.append(t) |
---|
| 91 | |
---|
| 92 | for t in threads: |
---|
| 93 | t.join() |
---|
| 94 | |
---|