0

I know when you want to send HTTP requests in Python 3 , you use the following code:

import requests
requests.get(url = "http://example.com")

and the program waits for the website to send back it's status / source code. But I'm trying to write a program that sends HTTP requests but doesn't wait for the website to reply.How can I do that?

EDIT I tried to use TCP and UDP but neither works since I get the status of the website after the packets are being sent.

Borna Ghahnoosh
  • 103
  • 1
  • 8

1 Answers1

1

If you dont care about the response at all, you can use multiprocessing pool for this.

import multiprocessing
import requests

p = multiprocessing.Pool(processes = 1)
p.apply_async(requests.get, ['https://google.com'])

I assume the only purpose you would want to do this is when you want to make asynchronous calls without wating for a call to finish to start another so you can increase the number of processes and call asynchronously in a for loop as:

import multiprocessing
import requests

p = multiprocessing.Pool(processes = 4)
for i in range(10):
    p.apply_async(requests.get, ['https://google.com'])

Just an example, number of processes and async call can vary based on your processor capability.

MohitC
  • 4,541
  • 2
  • 34
  • 55