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

[Python] FastAPI python: How to run a thread in the background?

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 05:52.

  1. Stack

    Stack Membro Participativo

    I'm making a server in python using FastAPI, and I want a function that is not related to my API, to run in background every 5 minutes (like checking stuff from an API and printing stuff depending on the response)

    I've tried to make a thread that runs the function start_worker, but it doesn't print anything.

    Does anyone know how to do so ?

    def start_worker():
    print('[main]: starting worker...')
    my_worker = worker.Worker()
    my_worker.working_loop() # this function prints "hello" every 5 seconds

    if __name__ == '__main__':
    print('[main]: starting...')
    uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)
    _worker_thread = Thread(target=start_worker, daemon=False)
    _worker_thread.start()

    Continue reading...

Compartilhe esta Página