1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How can I kill a task at a specific time?

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I want to be sure that a task is killed at a certain time if it is still running. The context is an overloaded worker, where tasks are not picked up straight away.

    Image this "busy" worker (concurrency=1 to simulate business):

    uv run celery --app src.tasks.example worker --concurrency 1 --queues celery --loglevel info --hostname default


    I am going to run this task, which just sleeps for the number of seconds given in parameter:

    @app.task()
    def naptime(seconds: float) -> None:
    sleep(seconds)


    I submit this task that way:

    naptime.apply_async(args=(3,))
    task2 = naptime.apply_async(args=(3,))
    start = time()
    result = task2.get()
    total = time() - start


    I expect total to be around 6 (2 tasks of 3 seconds, which are executed sequentially) and it is indeed close enough.

    Now, I want to make sure that task2 is killed 5 seconds after submission time if it runs.


    • task2 = naptime.apply_async(args=(3,), time_limit=5) => does not work as I want, because it would kill the task 5 seconds after it is picked up The task is done by then, but later than 5 seconds after submission.


    • task2 = naptime.apply_async(args=(3,), time_limit=5, expires=5) => does not work as I want, the task does not have time to expire.

    Note: in reality, I do not know how long the task will last, so I can not compute an expires (task of S seconds that needs to be done T seconds after submission could have an expire of (T-S). I know T but not S.)

    How can I make sure that the task is killed at a specific time?

    Continue reading...

Compartilhe esta Página