Repository navigation
Expand file tree
/
Copy pathexample_async_vs_multithread.py
More file actions
64 lines (53 loc) · 1.69 KB
/
Copy pathexample_async_vs_multithread.py
File metadata and controls
64 lines (53 loc) · 1.69 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
import asyncio
import time
import psutil
from swarms.structs.agent import Agent
from swarms.prompts.finance_agent_sys_prompt import (
FINANCIAL_AGENT_SYS_PROMPT,
)
# Initialize the agent (no external imports or env lookups needed here)
agent = Agent(
agent_name="Financial-Analysis-Agent",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
max_loops=1,
autosave=True,
dashboard=False,
verbose=True,
dynamic_temperature_enabled=True,
saved_state_path="finance_agent.json",
user_name="swarms_corp",
retry_attempts=1,
context_length=200000,
return_step_meta=False,
output_type="string",
streaming_on=False,
model_name="gpt-5.4",
)
# Helper decorator to measure time and memory usage
def measure_time_and_memory(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
elapsed = time.time() - start
mem_mb = psutil.Process().memory_info().rss / 1024**2
print(
f"[{func.__name__}] Time: {elapsed:.2f}s | Memory: {mem_mb:.2f} MB"
)
return result
return wrapper
# Async wrapper using asyncio.to_thread for the blocking call
@measure_time_and_memory
async def run_agent_async():
return await asyncio.to_thread(
agent.run,
"How can I establish a ROTH IRA to buy stocks and get a tax break? What are the criteria?",
)
# Threaded wrapper simply runs the async version in the event loop again
@measure_time_and_memory
def run_agent_in_thread():
asyncio.run(run_agent_async())
if __name__ == "__main__":
# 1) Run asynchronously
asyncio.run(run_agent_async())
# 2) Then run again via the threaded wrapper
run_agent_in_thread()