Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
Python function to print a dot to console while HTTP call executes

I am somewhat new to Python. I have looked around but cannot find an answer that fits exactly what I am looking for.

I have a function that makes an HTTP call using the requests package. I'd like to print a '.' to the screen (or any char) say every 10 seconds while the HTTP requests executes, and stop printing when it finishes. So something like:

def make_call:  
  rsp = requests.Post(url, data=file)  
  while requests.Post is executing print('.')  
   

Of course the above code is just pseudo code but hopefully illustrates what I am hoping to accomplish.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Every function call from the requests module is blocking, so your program waits until the function returns a value. The simplest solution is to use the built-in threading library which was already suggested. Using this module allows you to use code "parallelism"*. In your example you need one thread for the request which will be blocked until the request finished and the other for printing.

If you want to learn more about more advanced solutions see this answer https://stackoverflow.com/a/14246030/17726897

Here's how you can achieve your desired functionality using the threading module

def print_function(stop_event):
    while not stop_event.is_set():
        print(".")
        sleep(10)


should_stop = threading.Event()
thread = threading.Thread(target=print_function, args=[should_stop])
thread.start() # start the printing before the request
rsp = requests.Post(url, data=file) # start the requests which blocks the main thread but not the printing thread
should_stop.set() # request finished, signal the printing thread to stop
thread.join() # wait for the thread to stop

# handle response

* parallelism is in quotes because of something like the Global Interpreter Lock (GIL). Code statements from different threads aren't executed at the same time.

over 4 years ago · Santiago Trujillo Denunciar

0

i don't really getting what you looking for but if you want two things processed at the same time you can use multithreading module Example:

import threading
import requests
from time import sleep

def make_Request():
    while True:
        req = requests.post(url, data=file)
        sleep(10)


makeRequestThread = threading.Thread(target=make_Request)
makeRequestThread.start()

while True:
    print("I used multi threading to do two tasks at a same time")
    sleep(10)

or you can use very simple schedule module to schedule your tasks in a easy way

docs: https://schedule.readthedocs.io/en/stable/#when-not-to-use-schedule

over 4 years ago · Santiago Trujillo Denunciar

0

import threading
import requests
from time import sleep

#### Function print and stop when answer comes ###    
def Print_Every_10_seconds(stop_event):
    while not stop_event.is_set():
        print(".")
        sleep(10)

### Separate flow of execution ###
Stop = threading.Event()
thread = threading.Thread(target=Print_Every_10_seconds, args=[Stop])

### Before the request, the thread starts printing ### 
thread.start()

### Blocking of the main thread (the print thread continues) ###
Block_thread_1 = requests.Post(url, data=file) 

### Thread stops ###
Stop.set() 
thread.join()
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda