I want to queue my ml predictions using rq. Example code (pesudo-ish):
predict.py
:
import tensorflow as tf
def predict_stuff(foo):
model = tf.load_model()
result = model.predict(foo)
return result
app.py
:
from rq import Queue
from redis import Redis
from predict import predict_stuff
q = Queue(connection=Redis())
for foo in baz:
job = q.enqueue(predict_stuff, foo)
worker.py
:
import sys
from rq import Connection, Worker
# Preload libraries
import tensorflow as tf
with Connection():
qs = sys.argv[1:] or ['default']
w = Worker(qs)
w.work()
I've read rq docs explaining that you can preload libraries to avoid importing them every time a job is run (so in example code I import tensorflow in the worker code). However, I also want to move model loading from predict_stuff
to avoid loading the model every time the worker runs a job. How can I go about that?
RQ ( Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers. It is backed by Redis and it is designed to have a low barrier to entry. It can be integrated in your web stack easily. RQ requires Redis >= 3.0.0. First, run a Redis server.
There are many task queues in Python to assist you in your project, however, we’ll be discussing a solution today known as RQ. RQ, also known as Redis Queue, is a Python library that allows developers to enqueue jobs to be processed in the background with workers.
There are a variety of tools to solve this issue, but RQ Scheduler is a lightweight, elegant solution built on top of another tool with a low barrier to entry called Redis Queue. Let’s walk through how to queue up a function that notifies you when the International Space Station is flying over your location using the ISS Open Notify API.
Download, test drive, and tweak them yourself. Executing specific code at specific times is a common task in Python. There are a variety of tools to solve this issue, but RQ Scheduler is a lightweight, elegant solution built on top of another tool with a low barrier to entry called Redis Queue.
I'm not sure if this is something that can help but, following the example here:
https://github.com/rq/rq/issues/720
Instead of sharing a connection pool, you can share the model.
pseudo code:
import tensorflow as tf
from rq import Worker as _Worker
from rq.local import LocalStack
_model_stack = LocalStack()
def get_model():
"""Get Model."""
m = _model_stack.top
try:
assert m
except AssertionError:
raise('Run outside of worker context')
return m
class Worker(_Worker):
"""Worker Class."""
def work(self, burst=False, logging_level='WARN'):
"""Work."""
_model_stack.push(tf.load_model())
return super().work(burst, logging_level)
def predict_stuff_job(foo):
model = get_model()
result = model.predict(foo)
return result
I use something similar to this for a "global" file reader I wrote. Load up the instance into the LocalStack and have the workers read off the stack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With