-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
42 lines (31 loc) · 1.11 KB
/
Copy pathmain.py
File metadata and controls
42 lines (31 loc) · 1.11 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
import asyncio
import functools
import random
from CroFlow import run_coros, threading_run_coros
async def fetch_data(url: str) -> str:
# Simulate fetching data from a URL
sleep_time = random.randint(1, 5)
if sleep_time == 5:
raise Exception("TO MUCH DOWN TIME")
await asyncio.sleep(sleep_time) # Simulate varying fetch times
return f"Data fetched from {url}"
async def main():
# List of URLs to fetch data from
urls = [
"https://example.com/data1",
"https://example.com/data2",
"https://example.com/data3",
"https://example.com/data4",
"https://example.com/data5",
"https://example.com/data6",
"https://example.com/data7",
"https://example.com/data8",
"https://example.com/data9",
"https://example.com/data10"
]
# Create a list of asynchronous tasks
tasks = [functools.partial(fetch_data, url) for url in urls]
# Run the tasks concurrently with a maximum of 3 process groups
async for result in run_coros(tasks, timeout=5, debug=True):
print(result, type(result))
asyncio.run(main())