Skip to content

Commit f5fabfc

Browse files
committed
Added uvloop for better async for UNIX systems
1 parent de1a5c6 commit f5fabfc

14 files changed

Lines changed: 128 additions & 76 deletions

benchmarks/bench_latency.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
2222

23+
import cortex
2324
from cortex.core.publisher import Publisher
2425
from cortex.core.subscriber import Subscriber
2526
from cortex.discovery.daemon import DiscoveryDaemon
@@ -103,9 +104,16 @@ async def subscriber_main():
103104
message_type=LatencyMessage,
104105
node_name="latency_subscriber",
105106
wait_for_topic=True,
106-
topic_timeout=10.0,
107+
topic_timeout=30.0,
107108
)
108109

110+
# Wait for topic to be available before signaling ready
111+
if not sub.is_connected:
112+
connected = await sub._async_connect()
113+
if not connected:
114+
results_queue.put({"received": 0, "latencies": [], "error": "timeout"})
115+
return
116+
109117
# Signal ready
110118
ready_event.set()
111119

@@ -138,7 +146,7 @@ async def subscriber_main():
138146
}
139147
)
140148

141-
asyncio.run(subscriber_main())
149+
cortex.run(subscriber_main())
142150

143151

144152
def run_latency_benchmark(

benchmarks/bench_throughput.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import numpy as np
1818

19+
import cortex
1920
from cortex import Publisher, Subscriber
2021
from cortex.discovery import DiscoveryDaemon
2122
from cortex.messages import ArrayMessage
@@ -121,7 +122,7 @@ async def run_sub():
121122
except Exception:
122123
break
123124

124-
asyncio.run(run_sub())
125+
cortex.run(run_sub())
125126

126127
sub_thread = threading.Thread(target=subscriber_loop, daemon=True)
127128
sub_thread.start()

examples/multi_node_system.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import numpy as np
2525

26+
import cortex
2627
from cortex import Message, Node
2728
from cortex.messages.base import MessageHeader
2829

@@ -236,4 +237,4 @@ async def main() -> None:
236237

237238
if __name__ == "__main__":
238239
with contextlib.suppress(KeyboardInterrupt):
239-
asyncio.run(main())
240+
cortex.run(main())

examples/publisher_dict.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import numpy as np
2424

25+
import cortex
2526
from cortex import DictMessage, Node
2627

2728

@@ -108,4 +109,4 @@ async def main() -> None:
108109

109110
if __name__ == "__main__":
110111
with contextlib.suppress(KeyboardInterrupt):
111-
asyncio.run(main())
112+
cortex.run(main())

examples/publisher_numpy.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
python examples/subscriber_numpy.py
1717
"""
1818

19-
import asyncio
20-
2119
import numpy as np
2220

21+
import cortex
2322
from cortex import ArrayMessage, Node
2423

2524

@@ -84,4 +83,4 @@ async def main():
8483

8584

8685
if __name__ == "__main__":
87-
asyncio.run(main())
86+
cortex.run(main())

examples/publisher_tensor.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
python examples/subscriber_tensor.py
1717
"""
1818

19-
import asyncio
20-
2119
try:
2220
import torch
2321
except ImportError:
2422
print("This example requires PyTorch. Install with: pip install torch")
2523
exit(1)
2624

25+
import cortex
2726
from cortex import Node, TensorMessage
2827

2928

@@ -84,4 +83,4 @@ async def main():
8483

8584

8685
if __name__ == "__main__":
87-
asyncio.run(main())
86+
cortex.run(main())

examples/subscriber_dict.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import asyncio
2020
import contextlib
2121

22+
import cortex
2223
from cortex import DictMessage, Node
2324
from cortex.messages.base import MessageHeader
2425

@@ -28,19 +29,17 @@ class DictSubscriberNode(Node):
2829

2930
def __init__(self) -> None:
3031
super().__init__(name="dict_subscriber")
32+
# Create a subscriber - connection happens asynchronously in run()
3133
print("Waiting for publisher on /robot/state...")
32-
sub = self.create_subscriber(
34+
self.create_subscriber(
3335
topic_name="/robot/state",
3436
message_type=DictMessage,
3537
callback=self._on_state_received,
3638
wait_for_topic=True,
3739
topic_timeout=30.0,
3840
)
3941

40-
if not sub.is_connected:
41-
raise RuntimeError("Failed to connect to topic!")
42-
43-
print("Connected! Receiving messages...")
42+
print("Subscriber created, will connect when run() is called...")
4443
print("Press Ctrl+C to stop")
4544
print()
4645

@@ -78,11 +77,7 @@ async def main() -> None:
7877
"""Run the dictionary subscriber example."""
7978
print("Starting dictionary subscriber...")
8079

81-
try:
82-
node = DictSubscriberNode()
83-
except RuntimeError as e:
84-
print(e)
85-
return
80+
node = DictSubscriberNode()
8681

8782
try:
8883
await node.run()
@@ -95,4 +90,4 @@ async def main() -> None:
9590

9691
if __name__ == "__main__":
9792
with contextlib.suppress(KeyboardInterrupt):
98-
asyncio.run(main())
93+
cortex.run(main())

examples/subscriber_numpy.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
python examples/subscriber_numpy.py
1717
"""
1818

19-
import asyncio
20-
19+
import cortex
2120
from cortex import ArrayMessage, Node
2221
from cortex.messages.base import MessageHeader
2322

@@ -41,7 +40,7 @@ class ArraySubscriberNode(Node):
4140
def __init__(self):
4241
super().__init__(name="array_subscriber")
4342

44-
# Create a subscriber
43+
# Create a subscriber - connection happens asynchronously in run()
4544
print("Waiting for publisher on /sensor/array_data...")
4645
self.sub = self.create_subscriber(
4746
topic_name="/sensor/array_data",
@@ -51,10 +50,7 @@ def __init__(self):
5150
topic_timeout=30.0,
5251
)
5352

54-
if not self.sub.is_connected:
55-
raise RuntimeError("Failed to connect to topic!")
56-
57-
print("Connected! Receiving messages...")
53+
print("Subscriber created, will connect when run() is called...")
5854
print("Press Ctrl+C to stop")
5955
print()
6056

@@ -63,17 +59,15 @@ async def main():
6359
"""Run the subscriber example."""
6460
print("Starting NumPy array subscriber...")
6561

62+
node = ArraySubscriberNode()
63+
6664
try:
67-
node = ArraySubscriberNode()
6865
await node.run()
69-
except RuntimeError as e:
70-
print(f"Error: {e}")
7166
except KeyboardInterrupt:
7267
print("\nShutting down...")
7368
finally:
74-
if "node" in locals():
75-
await node.close()
69+
await node.close()
7670

7771

7872
if __name__ == "__main__":
79-
asyncio.run(main())
73+
cortex.run(main())

examples/subscriber_tensor.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
python examples/subscriber_tensor.py
1717
"""
1818

19-
import asyncio
20-
2119
try:
2220
import torch
2321
except ImportError:
2422
print("This example requires PyTorch. Install with: pip install torch")
2523
exit(1)
2624

25+
import cortex
2726
from cortex import Node, TensorMessage
2827
from cortex.messages.base import MessageHeader
2928

@@ -50,7 +49,7 @@ class TensorSubscriberNode(Node):
5049
def __init__(self):
5150
super().__init__(name="tensor_subscriber")
5251

53-
# Create a subscriber
52+
# Create a subscriber - connection happens asynchronously in run()
5453
print("Waiting for publisher on /model/features...")
5554
self.sub = self.create_subscriber(
5655
topic_name="/model/features",
@@ -60,10 +59,7 @@ def __init__(self):
6059
topic_timeout=30.0,
6160
)
6261

63-
if not self.sub.is_connected:
64-
raise RuntimeError("Failed to connect to topic!")
65-
66-
print("Connected! Receiving messages...")
62+
print("Subscriber created, will connect when run() is called...")
6763
print("Press Ctrl+C to stop")
6864
print()
6965

@@ -73,17 +69,15 @@ async def main():
7369
print("Starting PyTorch tensor subscriber...")
7470
print(f"PyTorch version: {torch.__version__}")
7571

72+
node = TensorSubscriberNode()
73+
7674
try:
77-
node = TensorSubscriberNode()
7875
await node.run()
79-
except RuntimeError as e:
80-
print(f"Error: {e}")
8176
except KeyboardInterrupt:
8277
print("\nShutting down...")
8378
finally:
84-
if "node" in locals():
85-
await node.close()
79+
await node.close()
8680

8781

8882
if __name__ == "__main__":
89-
asyncio.run(main())
83+
cortex.run(main())

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ name = "cortex"
77
version = "0.1.0"
88
description = "A lightweight framework using ZeroMQ for inter-process communication"
99
readme = "README.md"
10-
license = {text = "MIT"}
10+
license = {text = "Apache-2.0"}
1111
requires-python = ">=3.10"
1212
authors = [
13-
{name = "Cortex Authors"}
13+
{name = "Richeek Das", email = "richeek@seas.upenn.edu"}
1414
]
1515
classifiers = [
1616
"Development Status :: 3 - Alpha",
@@ -20,11 +20,14 @@ classifiers = [
2020
"Programming Language :: Python :: 3.10",
2121
"Programming Language :: Python :: 3.11",
2222
"Programming Language :: Python :: 3.12",
23+
"Programming Language :: Python :: 3.13",
24+
"Programming Language :: Python :: 3.14",
2325
]
2426
dependencies = [
2527
"pyzmq>=27.0.0",
2628
"numpy>=1.26.4",
2729
"msgpack>=1.0.0",
30+
"uvloop>=0.19.0; sys_platform != 'win32'",
2831
]
2932

3033
[project.optional-dependencies]

0 commit comments

Comments
 (0)