-2

[edit] Python 3.6

There is a function:

def main(request):
    do_something() // task takes some days
    responce = {'status': 'started!'}
    return responce

I need it to return a responce right after do_something() started and NOT waiting for do_something() to be finished. I have already tried this:

def main(request):
    Thread(target=do_something).start()
    responce = {'status': 'started!'}
    return responce

It works only if do_something() doesn't contain threads inside. Please, help with the usage of async-await here 'without extra code lines'

[update] this works fine only in Jupyter, but not in docker container(there yields 'RuntimeError: There is no current event loop in thread'):

import asyncio

async def do_something():
    print("internal training started")
    await asyncio.sleep(5)
    print("internal training finished")

def main():
    asyncio.ensure_future(train_models())
    responce = {'status': 'started!'}
    return responce
print(main())

[update] the problem was not with threads ans asynchronous functions, the problem was with azure-functions. [closed]

Alexey Kolosov
  • 140
  • 2
  • 15
  • Could you please provide a slightly more complete example, with exactly what issues you're seeing? – Roy2012 May 26 '20 at 08:45
  • @Roy2012, When using Thread().start() with threads inside, it just 'dies' without any exceptions – Alexey Kolosov May 26 '20 at 08:48
  • Could it be that the program is just done, without waiting for one of the internal threads? – Roy2012 May 26 '20 at 08:49
  • No, bacause every my internal thread at the end creates some files, plus during the processing writes logs. There are only some first logs and no files at all. – Alexey Kolosov May 26 '20 at 08:52
  • so .. maybe the main thread is done, the program exits, and it just doesn't wait for any of the internal (other) threads to complete. Could that be the case? Have to tried using 'join' to wait for the other threads? – Roy2012 May 26 '20 at 08:54
  • Maybe it is so) I haven't tried to use join yet. I have read [link] (https://stackoverflow.com/questions/34680985/what-is-the-difference-between-asynchronous-programming-and-multithreading) and understood that i need to use async function instead of thread, but don't know how exactly – Alexey Kolosov May 26 '20 at 08:57

1 Answers1

1

Using asyncio:

import asyncio


async def do_something():
    for i in range(10):
        print(f"Been doing expensive stuff for {i} seconds.")
        await asyncio.sleep(1)


def handle_request(request):
    task = asyncio.create_task(do_something())
    response = {request: {"status": "started!"}}
    return response, task


async def main():
    response, task = handle_request("request")
    print(response)
    await task
    print("Program finished.")


if __name__ == "__main__":
    asyncio.run(main())

Output:

{'request': {'status': 'started!'}}
Been doing expensive stuff for 0 seconds.
Been doing expensive stuff for 1 seconds.
Been doing expensive stuff for 2 seconds.
Been doing expensive stuff for 3 seconds.
Been doing expensive stuff for 4 seconds.
Been doing expensive stuff for 5 seconds.
Been doing expensive stuff for 6 seconds.
Been doing expensive stuff for 7 seconds.
Been doing expensive stuff for 8 seconds.
Been doing expensive stuff for 9 seconds.
Program finished.

Or alternatively using threading:

import time
from threading import Thread


def do_something():
    for i in range(10):
        print(f"Been doing expensive stuff for {i} seconds.")
        time.sleep(1)


def handle_request(request):
    thread = Thread(target=do_something)
    thread.start()
    response = {request: {"status": "started!"}}
    return response, thread


def main():
    response, thread = handle_request("request")
    print(response)
    thread.join()
    print("Program finished.")


if __name__ == "__main__":
    main()

Output:

Been doing expensive stuff for 0 seconds.
{'request': {'status': 'started!'}}
Been doing expensive stuff for 1 seconds.
Been doing expensive stuff for 2 seconds.
Been doing expensive stuff for 3 seconds.
Been doing expensive stuff for 4 seconds.
Been doing expensive stuff for 5 seconds.
Been doing expensive stuff for 6 seconds.
Been doing expensive stuff for 7 seconds.
Been doing expensive stuff for 8 seconds.
Been doing expensive stuff for 9 seconds.
Program finished.
ruohola
  • 21,987
  • 6
  • 62
  • 97
  • run(main()) not working, because I use python 3.6, plus it is preferably to do all work inside main function, because main() is Azure Function – Alexey Kolosov May 26 '20 at 10:06