Issue
async def work():
asyncio.sleep(3)
@router.get('')
async def test():
time1 = monotonic()
... # need to call work
time2 = monotonic()
return TestResponse(time=time2-time1)
work
function should be called only once at the same time. i need write test
function so if i call the endpoint several times at the same time i get responses: time
=time that differs from the previous call by at least 3 seconds.
Solution
I don't know about the timing, but you might want to have a look at asyncio.Lock
. Then you'd do something like
import asyncio
work_lock = asyncio.Lock()
async def work():
async with work_lock():
# Do your actual work
That should guarantee that if two requests call your endpoint, one will await the other.
Answered By - M.O.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.