S
S
s75002021-03-30 15:46:14
Python
s7500, 2021-03-30 15:46:14

How to correctly write elements from the RQ queue to the list?

I work with workers and queue

from rq import Queue
from redis import Redis


import time



redis_conn = Redis()
queue = Queue(connection=redis_conn)

list_int = []

def worker(num, timeout):
    time.sleep(timeout)
    list_int.append(num)

    return num


the problem is that when the worker gets queued, it contains a reference to an empty list
And if we start the workers with a frequency of 1 second, and they are given a timeout of 5 seconds, then they overwrite the list
That is, the elements are not added to the end of the list , but a list with one element is created
. I still don’t really understand how to solve this.
If someone has thoughts, I will be very grateful)

ZY . I forgot to show how it gets in line at all

from fastapi import FastAPI
@app.get("/api/{num}/{timeout}")
def read_root(num: int, timeout: int):
    job = queue.enqueue(worker, num, timeout)
    return {"Task": "in work"}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-03-30
@dmtrrr

Why use a global variable?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question