-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make delegator blocking with a waiting http server
- Loading branch information
Showing
1 changed file
with
39 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,48 @@ | ||
#!/usr/bin/env python | ||
import sys | ||
import subprocess | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
|
||
# Later the address (or rather the port) should be dynamic | ||
processing_server_address = "http://localhost:{{ OCRD_PS_PORT }}" | ||
processor_name = "{{ processor_name }}" | ||
|
||
args = list(sys.argv) | ||
STOP_WAITING_SERVER = False | ||
|
||
|
||
cmd = [ | ||
"ocrd", "network", "client", "processing", "processor", | ||
processor_name, "--address", processing_server_address | ||
] | ||
subprocess.run(cmd + args[1:]) | ||
class CallbackReceiver(BaseHTTPRequestHandler): | ||
"""Simple http-server to wait for a processor to finish working. | ||
The OCR-D processor is started with a callback-url which points to this server. The processor | ||
starts working in the background but the call is returned immediately. When the processor is | ||
finished it sends a request to the callback-url. The purpose of this server is to wait for the | ||
processor to finish and print its result. | ||
""" | ||
def do_POST(self): | ||
self.send_response(200) | ||
self.send_header("Content-Type", "text/plain") | ||
self.end_headers() | ||
self.wfile.write("finished".encode("utf-8")) | ||
len = int(self.headers.get("Content-Length", 0)) | ||
data = self.rfile.read(len).decode("utf-8") | ||
# TODO: how should the callback-content be handled/printed | ||
print(f"Processor finished: {data}") | ||
global STOP_WAITING_SERVER | ||
STOP_WAITING_SERVER = True | ||
|
||
|
||
def call_processor_and_wait(): | ||
server = HTTPServer(("0.0.0.0", 0), CallbackReceiver) | ||
callback_url = f"http://172.17.0.1:{server.server_address[1]}" | ||
cmd = [ | ||
"ocrd", "network", "client", "processing", "processor", | ||
processor_name, "--address", processing_server_address, | ||
"--callback-url", callback_url | ||
] | ||
args = list(sys.argv) | ||
subprocess.run(cmd + args[1:]) | ||
while not STOP_WAITING_SERVER: | ||
server.handle_request() | ||
|
||
|
||
if __name__ == "__main__": | ||
call_processor_and_wait() |