JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.46k stars 187 forks source link

what's the correct way of using asyncio with PyCall #953

Closed AbhimanyuAryan closed 2 years ago

AbhimanyuAryan commented 2 years ago

Following this issues says PyCall doesn't support Tasks Channels. Given the python call. I'm not sure how to await a task and define a function as async

Python code snippet that I am trying to convert

import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()
        await page.goto("http://playwright.dev")
        print(await page.title())
        await browser.close()

asyncio.run(main())

Julia code:

using PyCall
asyncio = pyimport("asyncio")
async_playwright = pyimport("playwright.async_api").async_playwright

_____ function main()
  @pywith async_playwright() as p begin
        browser = p.chromium.launch()
        ____
end

asyncio.run(main())

do we have some sort of @pyasync @pyawait macros defined? Or some other solution?

AbhimanyuAryan commented 2 years ago

@stevengj @tkf is wrapping the function around py""" async def main(): .... """ only solution to get async await working? Or there's some other Julia solution?

Following this: https://github.com/JuliaPy/PyCall.jl/issues/599#issue-372185055

Quoting: "Some special syntaxes like async/await are not supported in PyCall"

JinraeKim commented 2 years ago

@stevengj @tkf is wrapping the function around py""" async def main(): .... """ only solution to get async await working? Or there's some other Julia solution?

Following this: https://github.com/JuliaPy/PyCall.jl/issues/599#issue-372185055

Quoting: "Some special syntaxes like async/await are not supported in PyCall"

Have you found an working example?

AbhimanyuAryan commented 2 years ago

@JinraeKim I don't think there's one. Skimmed though entire code base