lfoppiano commited on
Commit
1017e87
·
verified ·
1 Parent(s): 43d95ee

Update service.py

Browse files
Files changed (1) hide show
  1. service.py +35 -22
service.py CHANGED
@@ -2,46 +2,59 @@ import http.server
2
  import socketserver
3
  import subprocess
4
  import threading
 
5
 
6
  PORT = 8080
7
  command_executed = False
8
  command_output = []
 
9
 
10
- def run_command():
11
  global command_output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  commands = [
13
  ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/PMC_sample_1943', '-Prun=1', '-PfileRatio=1'],
14
  ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/PLOS_1000', '-Prun=1', '-PfileRatio=1'],
15
  ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/biorxiv-10k-test-2000', '-Prun=1', '-PfileRatio=1'],
16
  ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/eLife_984', '-Prun=1', '-PfileRatio=1']
17
  ]
18
-
19
  for command in commands:
20
- process = subprocess.Popen(
21
- command,
22
- stdout=subprocess.PIPE,
23
- stderr=subprocess.PIPE,
24
- text=True
25
- )
26
- while True:
27
- output = process.stdout.readline()
28
- if output == '' and process.poll() is not None:
29
- break
30
- if output:
31
- command_output.append(output)
32
- stderr_output = process.stderr.read()
33
- if stderr_output:
34
- command_output.append(stderr_output)
35
- process.stdout.close()
36
- process.stderr.close()
37
- process.wait()
38
 
39
  class Handler(http.server.SimpleHTTPRequestHandler):
40
  def do_GET(self):
41
  global command_executed, command_output
42
  if not command_executed:
43
  command_executed = True
44
- threading.Thread(target=run_command).start()
45
  response = "Command is being executed."
46
  else:
47
  response = "Command output:\n" + "".join(command_output)
@@ -53,4 +66,4 @@ class Handler(http.server.SimpleHTTPRequestHandler):
53
 
54
  with socketserver.TCPServer(("", PORT), Handler) as httpd:
55
  print(f"Serving on port {PORT}")
56
- httpd.serve_forever()
 
2
  import socketserver
3
  import subprocess
4
  import threading
5
+ import queue
6
 
7
  PORT = 8080
8
  command_executed = False
9
  command_output = []
10
+ command_queue = queue.Queue()
11
 
12
+ def run_command(command):
13
  global command_output
14
+ process = subprocess.Popen(
15
+ command,
16
+ stdout=subprocess.PIPE,
17
+ stderr=subprocess.PIPE,
18
+ text=True
19
+ )
20
+ while True:
21
+ output = process.stdout.readline()
22
+ if output == '' and process.poll() is not None:
23
+ break
24
+ if output:
25
+ command_output.append(output)
26
+ stderr_output = process.stderr.read()
27
+ if stderr_output:
28
+ command_output.append(stderr_output)
29
+ process.stdout.close()
30
+ process.stderr.close()
31
+ process.wait()
32
+
33
+ def command_worker():
34
+ while True:
35
+ command = command_queue.get()
36
+ if command is None:
37
+ break
38
+ run_command(command)
39
+ command_queue.task_done()
40
+
41
+ def start_commands():
42
  commands = [
43
  ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/PMC_sample_1943', '-Prun=1', '-PfileRatio=1'],
44
  ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/PLOS_1000', '-Prun=1', '-PfileRatio=1'],
45
  ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/biorxiv-10k-test-2000', '-Prun=1', '-PfileRatio=1'],
46
  ['./gradlew', 'jatsEval', '-Pp2t=/opt/grobid/evaluation/eLife_984', '-Prun=1', '-PfileRatio=1']
47
  ]
 
48
  for command in commands:
49
+ command_queue.put(command)
50
+ threading.Thread(target=command_worker).start()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  class Handler(http.server.SimpleHTTPRequestHandler):
53
  def do_GET(self):
54
  global command_executed, command_output
55
  if not command_executed:
56
  command_executed = True
57
+ threading.Thread(target=start_commands).start()
58
  response = "Command is being executed."
59
  else:
60
  response = "Command output:\n" + "".join(command_output)
 
66
 
67
  with socketserver.TCPServer(("", PORT), Handler) as httpd:
68
  print(f"Serving on port {PORT}")
69
+ httpd.serve_forever()