A
A
asinco2021-12-30 12:48:13
Python
asinco, 2021-12-30 12:48:13

How to run two asynchronous functions?

Maybe a stupid question, but I ask you to answer it. Just started learning asyncio Let

's say I have the following code:

async def test_1(message):
    await print(message + " 2")


async def test_2(message):
    await print(message)
    await test_1(message)


test_2("Hello")

If I run it, then the first asynchronous function calls another asynchronous function and an error occurs in the console: RuntimeWarning: Enable tracemalloc to get the object allocation traceback

What if I really really need to somehow call some function outside the asynchronous one? For example, if I write a bot on aiorgam, there are just such problems often

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Markin, 2021-12-30
@Meoniz

Print is not an asynchronous function. In your case, it might work like this:

import asyncio


async def test_1(message):
        print(message + " 2")


async def test_2(message):
        print(message)
        await test_1(message)


asyncio.run(test_2("Привет!"))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question