1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Asyncio .create_task() not running function passed in

Discussão em 'Python' iniciado por Stack, Outubro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a program that does several timer-related things in python and I need to make it so Asyncio creates a task (without waiting for it) by calling another function with asyncio.get_event_loop().create_task(timer_function(my_parameters)), I've used this before in another project and it worked just fine, however, in this case, it ends up not calling timer_function() like it should and I suspect that it happens because it's inside loops or something related to the project structure. I could not find anything that worked as for now, only using await managed to call the function, but that ended up not making it run in parallel. The project structure is as follows:

    async def timer_function(my_parameters):

    print('Timer_Function called')

    # Do stuff with the parameters

    asyncio.sleep(time_based_on_those_parameters)

    # Finish doing some other things



    # Note: final() doesn't need to be async, I only made it so
    # to try and test some fixes
    async def final(parameters):

    # Do stuff

    while True: # This part loops forever every minute

    # Do stuff

    for i in range(my_range):

    if some_condition_a:

    asyncio.get_event_loop().create_task(timer_function(my_parameters))

    print('Condition A met')

    if some_condition_b:

    asyncio.get_event_loop().create_task(timer_function(some_different_parameters)

    print('Condition B met')

    # Do some other stuff

    sleep(60)



    Once I run the code, all that gets printed when those conditions are met is

    >>> Condition met

    but what I expected to see is both

    >>> Condition met
    >>> Timer function called


    I then put await before the create_task part all that gets printed at the time is

    >>> Timer function called


    And then only when the timer runs out and does what it needs to do is when >>> Condition met gets printed. Is there a way to change this structure to accomodate Asyncio or something else I could try?

    EDIT: I found a workaround using threading instead of asyncio. The code is now like this:

    def timer_function(my_parameters): # Sync method now

    print('Timer_Function called')

    # Do stuff with the parameters

    sleep(time_based_on_those_parameters) # No longer asyncio.sleep()

    # Finish doing some other things



    def final(parameters):

    # Do stuff

    threads = []

    while True: # This part loops forever every minute

    # Do stuff

    for i in range(my_range):

    if some_condition_a:

    t = threading.Thread(target=timer_function, args=(my_parameters))
    threads.append(t)
    t.start()

    print('Condition A met')

    if some_condition_b:

    t = threading.Thread(target=timer_function, args=(my_parameters))
    threads.append(t)
    t.start()

    print('Condition B met')

    # Do some other stuff

    sleep(60)



    This now works as intended, so for me I no longer need to fix this issue, however if anyone knows why Asyncio doesn't do that when in this structure please let me know, as someone may have this same issue in the future. (I checked on another project I made and asyncio.get_event_loop().create_task(timer_function(my_parameters)) can be called without awaiting, the difference is that in this case it's inside a while True and a for loop, and on that case that worked it was simply called once on an event listener)

    Continue reading...

Compartilhe esta Página