Repository navigation
Expand file tree
/
Copy pathcompare.py
More file actions
70 lines (50 loc) · 1.64 KB
/
Copy pathcompare.py
File metadata and controls
70 lines (50 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import time
import threading
import multiprocessing
import asyncio
import aiohttp
URL = 'http://127.0.0.1:5000/api/data'
NUM_REQUESTS = 10
# thread
def thread_request():
response = requests.get(URL)
print(response.json())
def test_multithreading():
threads = []
start_time = time.time()
for _ in range(NUM_REQUESTS):
thread = threading.Thread(target=thread_request)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
print(f'Time taken with threading: {time.time() - start_time} seconds')
# process
def process_request():
response = requests.get(URL)
print(response.json())
def test_multiprocessing():
processes = []
start_time = time.time()
for _ in range(NUM_REQUESTS):
process = multiprocessing.Process(target=process_request)
processes.append(process)
process.start()
for process in processes:
process.join()
print(f'Time taken with multiprocessing: {time.time() - start_time} seconds')
# coroutine
async def async_request(session):
async with session.get(URL) as response:
print(await response.json())
async def test_asyncio():
async with aiohttp.ClientSession() as session:
tasks = [async_request(session) for _ in range(NUM_REQUESTS)]
start_time = time.time()
await asyncio.gather(*tasks)
print(f'Time taken with asyncio: {time.time() - start_time} seconds')
if __name__ == '__main__':
test_multithreading()
test_multiprocessing()
asyncio.run(test_asyncio())