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

New pattern for async view classes #1512

Closed
wants to merge 4 commits into from
Closed

New pattern for async view classes #1512

wants to merge 4 commits into from

Conversation

simonw
Copy link
Owner

@simonw simonw commented Nov 16, 2021

Refs #878 - starting out with the new AsyncBase class implementing a pytest-inspired asyncio parallel execution mechanism.

@simonw
Copy link
Owner Author

simonw commented Nov 16, 2021

I've decided to make the clever asyncio dependency injection opt-in - so you can either decorate with @inject or you can set inject_all = True on the class - for example:

import asyncio
from datasette.utils.asyncdi import AsyncBase, inject


class Simple(AsyncBase):
    def __init__(self):
        self.log = []

    @inject
    async def two(self):
        self.log.append("two")

    @inject
    async def one(self, two):
        self.log.append("one")
        return self.log

    async def not_inject(self, one, two):
        return one + two


class Complex(AsyncBase):
    inject_all = True

    def __init__(self):
        self.log = []

    async def b(self):
        self.log.append("b")

    async def a(self, b):
        self.log.append("a")

    async def go(self, a):
        self.log.append("go")
        return self.log

@codecov
Copy link

codecov bot commented Nov 16, 2021

Codecov Report

Merging #1512 (8f757da) into main (0156c6b) will decrease coverage by 2.10%.
The diff coverage is 36.20%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1512      +/-   ##
==========================================
- Coverage   91.82%   89.72%   -2.11%     
==========================================
  Files          34       36       +2     
  Lines        4430     4604     +174     
==========================================
+ Hits         4068     4131      +63     
- Misses        362      473     +111     
Impacted Files Coverage Δ
datasette/utils/vendored_graphlib.py 0.00% <0.00%> (ø)
datasette/utils/asyncdi.py 96.92% <96.92%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0156c6b...8f757da. Read the comment docs.

@simonw
Copy link
Owner Author

simonw commented Nov 16, 2021

E     File "/home/runner/work/datasette/datasette/datasette/utils/vendored_graphlib.py", line 56
E       if (result := self._node2info.get(node)) is None:
E                  ^
E   SyntaxError: invalid syntax

Oh no - the vendored code I use has := so doesn't work on Python 3.6! Will have to backport it more thoroughly.

@simonw
Copy link
Owner Author

simonw commented Nov 16, 2021

I made the changes locally and tested them with Python 3.6 like so:

cd /tmp
mkdir v
cd v
pipenv shell --python=python3.6
cd ~/Dropbox/Development/datasette
pip install -e '.[test]'
pytest tests/test_asyncdi.py

simonw added a commit to simonw/til that referenced this pull request Nov 16, 2021
@simonw
Copy link
Owner Author

simonw commented Nov 17, 2021

I'm going to extract out the asyncinject stuff into a separate library.

simonw added a commit to simonw/asyncinject that referenced this pull request Nov 17, 2021
@simonw
Copy link
Owner Author

simonw commented Nov 17, 2021

https://github.com/simonw/asyncinject version 0.1a0 is now live on PyPI: https://pypi.org/project/asyncinject/

@simonw
Copy link
Owner Author

simonw commented Nov 17, 2021

Closing this PR because I shipped the code in it as a separate library instead.

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.

None yet

1 participant