0

I'm making a lisp dialect & I can't use any special syntax, only functions. I'm trying to add asynchronous programming.

I can turn a function into an asynchronous one using asyncio.coroutine.

asyncio.run(asyncio.coroutine(lambda: print('Hello, world!'))())

I can also use asynchronous functions inside its body.

asyncio.run(asyncio.coroutine(lambda: asyncio.sleep(1))())

but the problem with that is that they will run in the background. So if by using a do block, I execute asyncio.sleep(1) two times, the program will wait only 1 second since both of them are running in the background.

This is when await/yield from is used in asynchronous programming, but it's a syntax & not a function, hence I can't use it. I'm looking for a function alternative.

ChatGPT couldn't solve it & I've taken a look at the following StackOverFlow posts.

  1. How can I call an async function without await?
  2. How to use await in a python lambda
  3. Can a Python coroutine be implemented without await or yield?
  4. Automatic conversion of standard function into asynchronous function in Python
  5. How to await a function in a normal function
  • What do you mean by "I can turn a function into an asynchronous one using `asyncio.coroutine`?" There is no function `asyncio.coroutine`. There is no point in turning a sync function into an async function unless there is something for the function to `await`. Sync functions and async functions are different in design and purpose. The async def declaration isn't what's important about an async function, it's the `await` expression inside the function. Could you clarify this? – Paul Cornelius Mar 22 '23 at 00:39
  • @PaulCornelius In the examples above `lambda: print('Hello, world!')` is a normal function, but using the `asyncio.coroutine` it will turn to an async one that will run in the background, without blocking – KianFakheriAghdam Mar 22 '23 at 05:29
  • @PaulCornelius `coroutine` is a function in `asyncio` & it was used a lot with the `yield from` syntax, before python added the async/await syntax – KianFakheriAghdam Mar 22 '23 at 05:30
  • @PaulCornelius I'm looking for an alternative of `await`/`yield from` that has the same semantics, but it's not a syntax, rather function – KianFakheriAghdam Mar 22 '23 at 05:31
  • @PaulCornelius I've already find replacement for `async` keyword. now I need one for `await` – KianFakheriAghdam Mar 22 '23 at 05:32
  • I see that asyncio.coroutine function was removed in Python3.11, which is the documentation I was looking at. There is a similar function `types.coroutine`, but it converts generator functions into coroutines. So, how is `asyncio.run(asyncio.coroutine(lambda: print('Hello, world!'))())` different from simply `print("Hello world")`? You say it runs in the background, but what background is that? The coroutine passed to asyncio.run() will start and end before your code advances to the next line. Since it is a sync function in the first place, no multitasking can occur. – Paul Cornelius Mar 22 '23 at 10:48

1 Answers1

0

I think what you're looking for is the asyncio.run() function. Basically, it is await outside of asynchronous functions. For example,

import asyncio

async def coro():
    await asyncio.sleep(1)
    print('hello')

asyncio.run(coro())
asyncio.run(coro())

will run both coroutines, one after another. It works the exact same way as await. For lambda functions, since asyncio.coroutine(...) yields a coroutine, you can use asyncio.run(asyncio.coroutine(...)) to get your desired result.

Hope that helps.