Fix not picking up result from falsy result (#107)

* Fix not picking up result from falsy result

* add test and docs

* typo
This commit is contained in:
Stan Triepels
2023-09-01 02:36:15 +02:00
committed by GitHub
parent a08ef61ba5
commit d8cc0cc4f4
4 changed files with 31 additions and 2 deletions

View File

@@ -148,7 +148,7 @@ def result(task_id, wait=0, cached=Conf.CACHED):
start = time()
while True:
r = Task.get_result(task_id)
if r:
if r is not None:
return r
if (time() - start) * 1000 >= wait >= 0:
break

View File

@@ -46,6 +46,10 @@ def hello():
return "hello"
def return_falsy_value():
return []
def result(obj):
print(f"RESULT HOOK {obj.name} : {obj.result()}")

View File

@@ -141,6 +141,30 @@ def test_cluster(broker):
broker.delete_queue()
@pytest.mark.django_db
def test_results(broker):
broker.list_key = "cluster_test:q"
broker.delete_queue()
a = async_task(
"django_q.tests.tasks.return_falsy_value",
broker=broker,
)
task_queue = Queue()
stop_event = Event()
stop_event.set()
pusher(task_queue, stop_event, broker=broker)
task_queue.put("STOP")
result_queue = Queue()
worker(task_queue, result_queue, Value("f", -1))
result_queue.put("STOP")
monitor(result_queue)
# should not loop indefinitely when a real value is returned
value = result(a, wait=-1)
assert value == []
@pytest.mark.django_db
def test_enqueue(broker, admin_user):
broker.list_key = "cluster_test:q"

View File

@@ -121,6 +121,7 @@ Optionally you can use the :class:`AsyncTask` class to instantiate a task and ke
a.run()
# wait indefinitely for the result and print it
# don't let the task return `None` or it will wait indefinitely
print(a.result(wait=-1))
# change the args
@@ -264,7 +265,7 @@ Reference
Gets the result of a previously executed task
:param str task_id: the uuid or name of the task
:param int wait: optional milliseconds to wait for a result. -1 for indefinite
:param int wait: optional milliseconds to wait for a result. -1 for indefinite, but be sure the result will not be `None` otherwise it will wait indefinitely!
:param bool cached: run this against the cache backend.
:returns: The result of the executed task