Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop.call_later(0) and Loop.call_later(-1) return a uvloop.loop.Handle and not an asyncio.TimerHandle #482

Open
graingert opened this issue Jul 20, 2022 · 2 comments

Comments

@graingert
Copy link
Contributor

  • uvloop version: master
  • Python version: 3.10
  • Platform: ubuntu
  • Can you reproduce the bug with PYTHONASYNCIODEBUG in env?: yes
  • Does uvloop behave differently from vanilla asyncio? How?: yes
Python 3.10.5 (main, Jun 11 2022, 16:53:24) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> import uvloop
>>> uvloop.new_event_loop().call_later(-1, lambda: None)
<Handle <lambda>>
>>> asyncio.new_event_loop().call_later(-1, lambda: None)
<TimerHandle when=79332.145640902 <lambda>() at <stdin>:1>
>>> 

the bug is here:

uvloop/uvloop/loop.pyx

Lines 1327 to 1328 in 5926386

if when == 0:
return self._call_soon(callback, args, context)

@graingert graingert changed the title Loop.call_later(0) and Loop.call_later(-1) return a uvloop.Handle and not an asyncio.TimerHandle Loop.call_later(0) and Loop.call_later(-1) return a uvloop.loop.Handle and not an asyncio.TimerHandle Jul 20, 2022
@graingert
Copy link
Contributor Author

graingert commented Jul 29, 2022

@fantix I think the issue here is that uvloop should not use uv_idle_callback for call_soon,

Instead uvloop should schedule a timer for running with uv_timer_init set at -1 called "call_soon_handle"

for the loop.call_soon calls, whenever there are items added to the self._ready deque uvloop should start the "call_soon_handle", when the timer fires (note it will always fire first because it is the only timer scheduled at -1) uvloop should then call all the recently added handles on the ready queue. If there are handles added to the ready queue at this point restart the call_soon_handle again.

for loop.call_later calls scheduled in the past uvloop should maintain an internal "self._call_later_expired". whenever call_soon_handle is called after calling all of the "_ready" callbacks uvloop should clear the original self._call_later_expired heap and then call all the handles in a copy of the call_later_expired heap

for call_later in the future the behavior can remain the same

@graingert
Copy link
Contributor Author

graingert commented Jul 29, 2022

import asyncio
import uvloop
from functools import partial

async def main():
    loop = asyncio.get_running_loop()
    loop.call_later(-0.5, partial(print, "4", end=""))
    loop.call_later(-1, partial(print, "3", end=""))
    loop.call_soon(partial(print, "1", end=""))
    await asyncio.sleep(0)
    print("2", end="")


print("asyncio")
asyncio.run(main())
print()
uvloop.install()
print("uvloop")
asyncio.run(main())
print()


# asyncio
# 1234
# uvloop
# 4312

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant