Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda