Skip to content

Commit

Permalink
Change the behavior of AsyncTimer() to wall-time
Browse files Browse the repository at this point in the history
  • Loading branch information
dkang-quora committed Apr 18, 2024
1 parent 4628dfc commit 56d8be8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 3 additions & 1 deletion asynq/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def __enter__(self):
return self

def __exit__(self, ty, value, tb):
if not is_asyncio_mode():
if is_asyncio_mode():
self.pause()
else:
leave_context(self, self._active_task)
self.pause()
del self._active_task
Expand Down
12 changes: 6 additions & 6 deletions asynq/tests/test_asynq_to_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,33 @@ async def blocking_op():
await asyncio.sleep(0.1)

@asynq.asynq()
def f1():
def f1(): # 500ms
with AsyncTimer() as timer:
time.sleep(0.1)
t1, t2 = yield f2.asynq()
time.sleep(0.1)
return timer.total_time, t1, t2

@asynq.asynq()
def f2():
def f2(): # 300ms
with AsyncTimer() as timer:
time.sleep(0.1)
t = yield f3.asynq()
time.sleep(0.1)
return timer.total_time, t

@asynq.asynq()
def f3():
def f3(): # 100ms
with AsyncTimer() as timer:
# since AsyncTimer is paused on blocking operations,
# the time for TestBatch is not measured
yield [blocking_op(), blocking_op()]
return timer.total_time

t1, t2, t3 = asyncio.run(f1.asyncio())
assert_eq(400000, t1, tolerance=10000) # 400ms, 10us tolerance
assert_eq(200000, t2, tolerance=10000) # 200ms, 10us tolerance
assert_eq(000000, t3, tolerance=10000) # 0ms, 10us tolerance
assert_eq(500000, t1, tolerance=10000) # 400ms, 10us tolerance
assert_eq(300000, t2, tolerance=10000) # 200ms, 10us tolerance
assert_eq(100000, t3, tolerance=10000) # 0ms, 10us tolerance


def test_method():
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ qcore
pygments
black==24.3.0
mypy==1.4.1
typing_extensions==4.11.0

0 comments on commit 56d8be8

Please sign in to comment.