Automatic code conversion from async to sync

5 pointsposted 7 hours ago
by dvarrazzo

2 Comments

dvarrazzo

7 hours ago

Psycopg 3 supports both sync and async code. Starting from release 3.2, the sync side is automatically generated from the async counterpart. This article explains how it's done, and the workflow used for the initial conversion to autogenerated code and for the regular maintenance.

PaulHoule

6 hours ago

You can accomplish a lot like this without parsing at the AST level as a lot of api calls are something like

  async def make_call(arguments):
     url = "..."
     body = process_arguments(arguments)
     response = await post(body)
     return process_response(response)
so you can ultimately write

  that_call = [url, process_arguments, process_response]
and then generate a bunch of sync and async closures that capture that_call and then call the right things. In the typical API that you might want to compose sync/async I find at least 90% can be generated that way but working at the AST level you can get the last bit.