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

Support for async with #1179

Open
ikappaki opened this issue Dec 15, 2024 · 0 comments
Open

Support for async with #1179

ikappaki opened this issue Dec 15, 2024 · 0 comments
Labels
component:basilisp.core Issue pertaining to basilisp.core namespace issue-type:enhancement New feature or request

Comments

@ikappaki
Copy link
Contributor

ikappaki commented Dec 15, 2024

Hi,

Python provides async with to with asynchronous context managers, it's like async but for asynchronous operations

For example, given the below file defining and calling with on an async context manager

awith.py

import asyncio

class AsyncContextManager:
    async def __aenter__(self):
        print("Entering")
        return self

    async def __aexit__(self, exc_type, exc, tb):
        print("Exiting")

async def main():
    async with AsyncContextManager() as manager:
        print("Inside")

if __name__ == "__main__":
    asyncio.run(main())

the output is

> python .\awith.py
Entering
Inside
Exiting

A simple with of course won't work in Basilisp
issuefor.lpy

(import asyncio awith)

(defasync main
  []
  (with [_ (awith/AsyncContextManager)]
        (println "inside")))

(asyncio/run (main))

it throws an error

> basilisp run .issuefor.lpy
Traceback (most recent call last):
  File "C:\\issuefor.lpy", line 6, in main
    (println "inside")))
              ^^^^^^^^^
AttributeError: 'AsyncContextManager' object has no attribute '__enter__'. Did you mean: '__aenter__'?

A naive work around is to directly call the manager enter/exit functions:

(defasync main
  []
  (let [acm (awith/AsyncContextManager)]
    (try
      (await (.__aenter__ acm))
      (println "inside")
      (finally
        (await (.__aexit__ acm nil nil nil))))))

Could support for async with be added? One possible approach might be to specify an :async meta key in the with metadata, as in (^:async with [...] ...).

Thanks

@chrisrink10 chrisrink10 added issue-type:enhancement New feature or request component:basilisp.core Issue pertaining to basilisp.core namespace labels Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:basilisp.core Issue pertaining to basilisp.core namespace issue-type:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants