Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

How can I run schedule inside a docker container that is also running Flask to serve API requests?

Aperta
#587 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
aws, docker, flask, python
Ambito
api, backend, cloud, devops

Direzione di ricerca

Non viene indicato alcun file del repository, test o punto di ingresso; l’issue fornisce solo un esempio Python con schedule.run_pending e descrive i vincoli di Flask, Docker e AWS. Inizia esaminando questo loop e il requisito di un singolo container, quindi determina se le attività pianificate non bloccanti e di lunga durata richieste sono supportate; per considerarlo completato sarebbe necessario un approccio funzionante e chiaramente documentato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I have python / Flask running in a Docker container deployed to a cloud service (AWS).

It responds to HTTP API requests (GET, POST)

Everything is working fine there with this.


Now I want to implement some recurring tasks that run inside the container using any Python scheduler or cron like package.

The recurring tasks may take 30 mins or an hour and will be running inside the container in parallel with responding to API requests without interruption.

I stumbled upon a Python library called 'schedule' (import schedule) and some sample code here

https://github.com/dbader/schedule

import schedule
from datetime import datetime

def job():

    print('\n---\njob()')
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print("Current Time =", current_time)
    print("I'm working...")

schedule.every(10).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(10)

You can see the problem here where the package requires a while True: loop If that is running when the container is instantiated, (in the loop always), the container will not respond to API requests.

Is there another scheduling package that does not require a dedicated loop running all the time?

It seems like I need something external that sleeps for so many seconds and then makes an API call to the container that would invoke:

schedule.run_pending()

but even then, this will not work because the job() running within an API call ( a long running task ), may take 30 mins to 1 hour to run and hit an HTTP time out.

How can I have an API server that responds to API requests, but also runs internally long running tasks initiated by an internal scheduler in the container?

I am not running under an EC2 or GCE instance where traditional cron can be used. I am using a service that simply hosts and runs a single container.

Requirements:

  • Docker container API server that responds to HTTP requests (GET, POST)
  • has the ability to run any number of tasks on a schedule that may be long running where the container will stay active and not be shut down or moved out of the cloud hosting service that is running it.

If you have actually done this and have done this with a working solution, please respond.

Lingua principale
Python
Stelle
12.3k
Fork
1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dbader/schedule

Tutte le issue di dbader/schedule

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.