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

Event loop is closed when using AsyncScheduler with RedisEventBroker #937

Closed
3 tasks done
de-shev opened this issue Jul 11, 2024 · 2 comments
Closed
3 tasks done
Labels

Comments

@de-shev
Copy link

de-shev commented Jul 11, 2024

Things to check first

  • I have checked that my issue does not already have a solution in the FAQ

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Version

4.0.0a5

What happened?

I'm encountering a RuntimeError: Event loop is closed when using AsyncScheduler with RedisEventBroker.

import asyncio
import logging
from datetime import datetime

from apscheduler import AsyncScheduler
from apscheduler.eventbrokers.redis import RedisEventBroker
from apscheduler.triggers.interval import IntervalTrigger
from redis.asyncio.client import Redis

from config import settings

def tick():
    print("Hello, the time is", datetime.now())

async def main():
    redis_url = f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}"
    event_broker = RedisEventBroker(client_or_url=Redis.from_url(redis_url))

    async with AsyncScheduler(event_broker=event_broker) as scheduler:
        await scheduler.add_schedule(tick, IntervalTrigger(seconds=1), id="tick")

logging.basicConfig(level=logging.INFO)
asyncio.run(main())

Error:

INFO:apscheduler._schedulers.async_:Added new schedule (task='__main__:tick', trigger=IntervalTrigger(seconds=1, start_time='2024-07-11 23:08:37.256226+07:00')); next run time at 2024-07-11 23:08:37.256226+07:00
Exception ignored in: <function StreamWriter.__del__ at 0x7f59ab40b060>
Traceback (most recent call last):
  File "/home/de_shev/.pyenv/versions/3.11.6/lib/python3.11/asyncio/streams.py", line 395, in __del__
    self.close()
  File "/home/de_shev/.pyenv/versions/3.11.6/lib/python3.11/asyncio/streams.py", line 343, in close
    return self._transport.close()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/de_shev/.pyenv/versions/3.11.6/lib/python3.11/asyncio/selector_events.py", line 860, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "/home/de_shev/.pyenv/versions/3.11.6/lib/python3.11/asyncio/base_events.py", line 761, in call_soon
    self._check_closed()
  File "/home/de_shev/.pyenv/versions/3.11.6/lib/python3.11/asyncio/base_events.py", line 519, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

And if I don't use the Redis object explicitly, but only the URL:

async def main():
    redis_url = f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}"
    event_broker = RedisEventBroker(client_or_url=redis_url)

    async with AsyncScheduler(event_broker=event_broker) as scheduler:
        await scheduler.add_schedule(tick, IntervalTrigger(seconds=1), id="tick")

I get the following error:

AttributeError: 'Redis' object has no attribute 'aclose'. Did you mean: 'close'?
Exception ignored in: <function StreamWriter.__del__ at 0x7f54aa40b060>

And by the way, is it possible to use the synchronous Scheduler with RedisEventBroker?

How can we reproduce the bug?

Run the provided script

@de-shev de-shev added the bug label Jul 11, 2024
@agronholm
Copy link
Owner

Are you sure you're running a recent version of the redis library? There certainly is an aclose() method.

@agronholm
Copy link
Owner

Closing due to lack of responses. Presumably the issue was resolved by upgrading redis.

@agronholm agronholm closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants