Skip to content

Commit

Permalink
Use rq as simple redis based queue
Browse files Browse the repository at this point in the history
  • Loading branch information
langdal committed Dec 2, 2021
1 parent 703d8a4 commit 10ddb27
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions optimizerapi/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
import numpy
numpy.random.seed(42)

from redis import Redis
from rq import Queue
import time

queue = Queue(connection=Redis())

plt.switch_backend('Agg')

"""ProcessOptimizer web request handler
Expand All @@ -33,6 +39,11 @@ def run(body) -> dict:
dict
a JSON encodable dictionary representation of the result.
"""
job = queue.enqueue(doRunWork, body)
while (job.return_value == None): time.sleep(0.2)
return job.return_value

def doRunWork(body) -> dict:
# print("Receive: " + str(body))
data = [(run["xi"], run["yi"]) for run in body["data"]]
cfg = body["optimizerConfig"]
Expand Down
8 changes: 8 additions & 0 deletions optimizerapi/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rq import Connection, Queue, Worker
from redis import Redis

if __name__ == '__main__':
# Tell rq what Redis connection to use
with Connection():
q = Queue(connection=Redis())
Worker(q).work()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ ProcessOptimizer==0.7.1
json-tricks==3.15.5
cryptography==3.4.7
waitress==2.0.0
rq==1.10.0

0 comments on commit 10ddb27

Please sign in to comment.