Created
February 15, 2024 10:13
-
-
Save xsa-dev/65db0d6830d2ea27fb396c8bc551114f to your computer and use it in GitHub Desktop.
Comparing TheadingPool and requests get method locked with GIL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# two cases 1 - multithreading | |
# two cases 2 - single threaded | |
import time | |
import logging | |
import requests | |
from concurrent.futures import ThreadPoolExecutor | |
import tqdm | |
# variables | |
url = "http://example.com" | |
threads_workers = 10 | |
test_iterations = 100 | |
# logger | |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s") | |
def get_request(url): | |
response = requests.get(url) | |
return response.status_code | |
def thread_pool_experiment(): | |
global url | |
global threads_workers | |
global test_iterations | |
with ThreadPoolExecutor(max_workers=threads_workers) as executor: | |
results = list(tqdm.tqdm(executor.map(get_request, [url] * test_iterations), total=test_iterations)) | |
for result in results: | |
if result != 200: | |
print(result) | |
def unparalleled_experiment(): | |
global url | |
global test_iterations | |
for _ in tqdm.tqdm(range(test_iterations)): | |
var = get_request(url) | |
if var != 200: | |
print(var) | |
if __name__ == "__main__": | |
# threaded requests | |
logging.info("start thread pool example") | |
threading_start = time.time() | |
thread_pool_experiment() | |
logging.info("end thread pool example") | |
threading_end = time.time() | |
logging.info(f"threading time: {threading_end - threading_start}") | |
logging.info("ok. make second experiment") | |
# locked requests | |
unparalleled_start = time.time() | |
logging.info(f"start unparalleled example {test_iterations} times") | |
unparalleled_experiment() | |
unparalleled_end = time.time() | |
logging.info("end unparalleled example") | |
# statistics: compare times between first and second experiment | |
logging.info("unparalleled time: " + str(unparalleled_end - unparalleled_start)) | |
unparalleled_time = unparalleled_end - unparalleled_start | |
paralleled_time = threading_end - threading_start | |
logging.info( | |
f"threading faster then unparalleled: {unparalleled_time // paralleled_time}" | |
) | |
""" | |
2024-02-15 13:09:37,714 - start thread pool example | |
100%|██████████| 100/100 [00:03<00:00, 33.27it/s] | |
2024-02-15 13:09:40,746 - end thread pool example | |
2024-02-15 13:09:40,747 - threading time: 3.032028913497925 | |
2024-02-15 13:09:40,747 - ok. make second experiment | |
2024-02-15 13:09:40,747 - start unparalleled example 100 times | |
100%|██████████| 100/100 [00:30<00:00, 3.27it/s] | |
2024-02-15 13:10:11,329 - end unparalleled example | |
2024-02-15 13:10:11,330 - unparalleled time: 30.58269500732422 | |
2024-02-15 13:10:11,330 - threading faster then unparalleled: 10.0 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment