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

SQLite cache: Use requests_cache.CachedSession for better concurrency #130

Merged
merged 1 commit into from
Mar 31, 2024

Conversation

amotl
Copy link
Contributor

@amotl amotl commented Mar 29, 2024

About

Details

Users on Apple M2 Max reported DB lock errors:

[grafana_wtf.core]
  INFO: Fetching dashboards in parallel with 5 concurrent requests

[requests_cache.backends.sqlite]
  WARNING: Database is locked in thread 6272069632; retrying (1/3)

Also segmentation faults, and leaks:

  UserWarning: resource_tracker: There appear to be 1 leaked semaphore
  objects to clean up at shutdown

NB: @Ousret, @JWCook: Just in case you didn't know yet -- Apparently, using Niquests together with Requests-Cache works well, at least according to the succeeding test suite.

Users on Apple M2 Max reported DB lock errors:

[grafana_wtf.core]
  INFO: Fetching dashboards in parallel with 5 concurrent requests

[requests_cache.backends.sqlite]
  WARNING: Database is locked in thread 6272069632; retrying (1/3)

Also segmentation faults, and leaks:

  UserWarning: resource_tracker: There appear to be 1 leaked semaphore
  objects to clean up at shutdown
Comment on lines +1 to +12
from sys import modules

import niquests
import urllib3

# Amalgamate the module namespace to make all modules aiming
# to use `requests`, in fact use `niquests` instead.
modules["requests"] = niquests
modules["requests.adapters"] = niquests.adapters
modules["requests.sessions"] = niquests.sessions
modules["requests.exceptions"] = niquests.exceptions
modules["requests.packages.urllib3"] = urllib3
Copy link
Contributor Author

@amotl amotl Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ousret: Could this monkeypatching / amalgamation code be also provided by Niquests itself, as a convenience shorthand? To be invoked, for example, like:

import niquests
niquests.patch_all()

Can't patch itself while it's already loaded, hm? Can it? We barely remember it, but isn't it the same with Gevent, like that... ?

import gevent
gevent.patch_all()

Edit: Ah, slightly similar, it is:

from gevent import monkey
monkey.patch_all()

-- https://www.gevent.org/api/gevent.monkey.html

wdyt?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this isn't the way to go. We should not tamper with sys.module outside of a development environment.

Try this instead

import requests_cache
import niquests


class CacheSession(requests_cache.session.CacheMixin, niquests.Session):
    ...


if __name__ == "__main__":

    s = CacheSession()

    for i in range(60):
        r = s.get('https://httpbin.org/delay/1')

While its still not ideal, it should work more accordingly.
I am thinking about forking popular extensions and see how we can ease this.

Copy link
Contributor Author

@amotl amotl Mar 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your advise. We will submit a patch to work with requests-cache in the way you are suggesting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converged that into a patch, thanks again.

@amotl amotl marked this pull request as ready for review March 29, 2024 18:56
Copy link

codecov bot commented Mar 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.68%. Comparing base (76e1abd) to head (5583ae0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #130      +/-   ##
==========================================
+ Coverage   86.50%   86.68%   +0.17%     
==========================================
  Files           8        9       +1     
  Lines        1060     1074      +14     
==========================================
+ Hits          917      931      +14     
  Misses        143      143              
Flag Coverage Δ
unittests 86.68% <100.00%> (+0.17%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@amotl amotl merged commit 6dc0015 into main Mar 31, 2024
14 checks passed
@amotl amotl deleted the amo/sqlite-concurrency branch March 31, 2024 00:56
Ousret added a commit to jawah/niquests that referenced this pull request Apr 2, 2024
Ousret added a commit to jawah/niquests that referenced this pull request Apr 2, 2024
Ousret added a commit to jawah/niquests that referenced this pull request Apr 2, 2024
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

Successfully merging this pull request may close these issues.

2 participants