1

I'm using asynchronous workflow with synchronous code within it. How do I check, if synchronous functions are blocking or not, to be sure that nothing breaks during execution.


async def download_upload_xmls_async(message,count,id,conn1,cursor1,conn2,cursor2):
    # SOME CODE
    xml = await req(urlX, headers)

    print('Got message')

    write(message_count, id, conn1, cursor1)
    print('Wrote progress')

    send_message("Send" + xml, id, conn2, cursor2)
    print('Sent message')

    write_locally(data)

    await message.ack()

In code above, how do I check, that functions write and send_message are non-blocking? They work with db, that i cannot access to check if everything works as expected. Also can I assume, that if function write_locally works correctly, that my previous functions also worked correctly?

Functions write and send_message do almost same thing - they take data and execute query on PostgreSQL db using connection and cursor passed to them. Function write_locally makes writes to csv file.

def send_message(message, id, con, cur, **nargs):
    params = {
              #some params
             }
    union_params= {**params, **nargs}
    data = json.dumps(union_params, ensure_ascii=False)
    cur.execute(
                #Query
                )
    con.commit()

Also i have to add, that connection and cursor were created with aiopg, so all their methods are coroutines.

1 Answers1

1

If the connection and cursor have coroutine methods, then send_message as written will not block the event loop.

However, it won't do anything because it fails to await the coroutines it calls. It needs to be defined with async def and it needs to await the calls to cur.execute(...) and cur.commit(). download_upload_xmls_async similarly fails to await send_message. Correct code should look like this:

async def download_upload_xmls_async(message, count, id, conn1, cursor1, conn2, cursor2):
    ... some code here ...
    # note the await
    await send_message("Send" + xml, id, conn2, cursor2)
    # write_locally doesn't need to be a coroutine because
    # it (presumably) doesn't block    
    write_locally(data)
    await message.ack()

# note "async def"
async def send_message(message, id, con, cur, **nargs):
    params = {
              #some params
             }
    union_params= {**params, **nargs}
    data = json.dumps(union_params, ensure_ascii=False)
    # note the await
    await cur.execute(
                #Query
                )
    await con.commit()
user4815162342
  • 141,790
  • 18
  • 296
  • 355