-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Implement RequestQueue class #25
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, looks pretty good! I had just a few small comments, but otherwise I think it's good to go.
It's crazy how much logic has to be there on top of our API, we should move this to the platform eventually.
from collections import OrderedDict | ||
from datetime import datetime | ||
from typing import Coroutine, Dict, Optional | ||
from typing import OrderedDict as OrderedDictType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't wait until we can deprecate Python 3.8 🙂
src/apify/storages/request_queue.py
Outdated
_recently_handled: LRUCache[bool] | ||
_assumed_total_count = 0 | ||
_assumed_handled_count = 0 | ||
_requests_cache: LRUCache[Dict] | ||
|
||
def __init__(self, id: str, name: Optional[str], client: Union[ApifyClientAsync, MemoryStorage]) -> None: | ||
"""TODO: docs (constructor should be "internal").""" | ||
self._id = id | ||
self._name = name | ||
self._client = client.request_queue(self._id, client_key=self._client_key) | ||
self._config = Configuration.get_global_configuration() # We always use the global config |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this actually used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, good point. It's not needed in the simplified storage manager version since it will use the global config if you don't provide config instance anyway.
src/apify/storages/request_queue.py
Outdated
""" | ||
if request is None: | ||
logging.debug(f'Cannot find a request from the beginning of queue, will be retried later. nextRequestId: {next_request_id}') | ||
asyncio.get_event_loop().call_later(STORAGE_CONSISTENCY_DELAY_MILLIS // 1000, lambda: self._in_progress.remove(next_request_id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not asyncio.get_running_loop()
? It's recommended over get_event_loop()
.
asyncio.get_event_loop().call_later(STORAGE_CONSISTENCY_DELAY_MILLIS // 1000, lambda: self._in_progress.remove(next_request_id)) | |
asyncio.get_running_loop().call_later(STORAGE_CONSISTENCY_DELAY_MILLIS // 1000, lambda: self._in_progress.remove(next_request_id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, did not know that
src/apify/storages/request_queue.py
Outdated
# Performance optimization: add request straight to head if possible | ||
self._maybe_add_request_to_queue_head(request['id'], forefront) | ||
|
||
asyncio.get_event_loop().call_later(STORAGE_CONSISTENCY_DELAY_MILLIS // 1000, callback) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same with the get_running_loop()
asyncio.get_event_loop().call_later(STORAGE_CONSISTENCY_DELAY_MILLIS // 1000, callback) | |
asyncio.get_running_loop().call_later(STORAGE_CONSISTENCY_DELAY_MILLIS // 1000, callback) |
Generic LRUCache implementing https://python.readthedocs.io/en/latest/library/collections.abc.html#collections.abc.MutableMapping
I want to add some validation to methods that have
request
as an argument. probably best way is to use Pydantic, we'll see...I also added a
Protocol
for Storages that can be used instead of the ugly union.Protocol
is basically implicit structural subtyping and seems to work ok https://peps.python.org/pep-0544/