Manager Class¶
cymongoose.Manager
¶
Manage Mongoose event loop and provide Python callbacks.
Source code in src/cymongoose/_mongoose.pyi
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 | |
connections
property
¶
Return a snapshot of all active connections as a tuple.
wakeup_id
property
¶
Connection ID of the internal wakeup pipe, or 0 if wakeup is not enabled.
__init__(handler=None, enable_wakeup=False, error_handler=None)
¶
Initialize event manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler
|
Optional[EventHandler]
|
Default event handler for all connections |
None
|
enable_wakeup
|
bool
|
Enable wakeup support for multi-threaded scenarios |
False
|
error_handler
|
Optional[Callable[[Exception], Any]]
|
Optional callback invoked with the exception when an event handler raises. Defaults to traceback.print_exc(). |
None
|
Source code in src/cymongoose/_mongoose.pyi
__exit__(*exc)
¶
Source code in src/cymongoose/_mongoose.pyi
poll(timeout_ms=0)
¶
Drive the event loop once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout_ms
|
int
|
Timeout in milliseconds (0 = non-blocking) |
0
|
Thread safety note: _freed flag is checked without lock. In multi-threaded scenarios, use close() only after all polling threads have stopped to avoid race conditions.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If manager has been freed |
Source code in src/cymongoose/_mongoose.pyi
listen(url, handler=None, *, http=None)
¶
Listen on a URL; handler is optional per-listener override.
The http flag is inferred from the URL scheme when not provided
explicitly: http://, https://, ws://, wss:// all
enable HTTP protocol parsing. Pass http=False to override.
To listen on an OS-assigned port, pass port 0 and read the actual port from the returned connection::
listener = mgr.listen("http://0.0.0.0:0")
port = listener.local_addr[1]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to listen on (e.g., "http://0.0.0.0:8000", "tcp://0.0.0.0:1234") |
required |
handler
|
Optional[EventHandler]
|
Optional per-connection handler (overrides default) |
None
|
http
|
Optional[bool]
|
Enable HTTP protocol handler (inferred from scheme if None) |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Listener connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to listen on URL |
Source code in src/cymongoose/_mongoose.pyi
connect(url, handler=None, *, http=None)
¶
Create an outbound connection and return immediately.
The http flag is inferred from the URL scheme when not provided
explicitly: http://, https://, ws://, wss:// all
enable HTTP protocol parsing. Pass http=False to override.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to connect to (e.g., "http://example.com", "tcp://example.com:1234") |
required |
handler
|
Optional[EventHandler]
|
Optional per-connection handler (overrides default) |
None
|
http
|
Optional[bool]
|
Enable HTTP protocol handler (inferred from scheme if None) |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to connect to URL |
Source code in src/cymongoose/_mongoose.pyi
ws_connect(url, handler=None)
¶
Create an outbound WebSocket connection.
Connects and automatically sends the WebSocket upgrade handshake. The handler will receive MG_EV_WS_OPEN when the handshake completes, then MG_EV_WS_MSG for incoming frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
WebSocket URL (e.g., 'ws://example.com/ws', 'wss://example.com/ws') |
required |
handler
|
Optional[EventHandler]
|
Optional per-connection handler (overrides default) |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to connect |
Source code in src/cymongoose/_mongoose.pyi
mqtt_connect(url, handler=None, client_id='', username='', password='', clean_session=True, keepalive=60)
¶
Connect to an MQTT broker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Broker URL (e.g., 'mqtt://broker.hivemq.com:1883') |
required |
handler
|
Optional[EventHandler]
|
Event handler callback |
None
|
client_id
|
str
|
MQTT client ID (autogenerated if empty) |
''
|
username
|
str
|
MQTT username (optional) |
''
|
password
|
str
|
MQTT password (optional) |
''
|
clean_session
|
bool
|
Clean session flag |
True
|
keepalive
|
int
|
Keep-alive interval in seconds |
60
|
Returns:
| Type | Description |
|---|---|
Connection
|
Connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to connect to broker |
Source code in src/cymongoose/_mongoose.pyi
mqtt_listen(url, handler=None)
¶
Listen for MQTT connections (broker mode).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Listen URL (e.g., 'mqtt://0.0.0.0:1883') |
required |
handler
|
Optional[EventHandler]
|
Event handler callback |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Listener connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to listen for MQTT |
Source code in src/cymongoose/_mongoose.pyi
sntp_connect(url, handler=None)
¶
Connect to an SNTP (time) server.
Triggers MG_EV_SNTP_TIME event when time is received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
SNTP server URL (e.g., 'udp://time.google.com:123') |
required |
handler
|
Optional[EventHandler]
|
Event handler callback |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to connect to SNTP server |
Example
def time_handler(conn, ev, data): if ev == MG_EV_SNTP_TIME: # data is uint64_t epoch milliseconds print(f"Time: {data} ms since epoch")
conn = manager.sntp_connect("udp://time.google.com:123", time_handler) conn.sntp_request() # Request time
Source code in src/cymongoose/_mongoose.pyi
wakeup(connection_id, data=b'')
¶
Send a wakeup notification to a specific connection (thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id
|
int
|
The connection ID to wake up |
required |
data
|
bytes
|
Optional data payload (delivered via MG_EV_WAKEUP event) |
b''
|
Returns:
| Type | Description |
|---|---|
bool
|
True if wakeup was sent successfully |
Thread safety note: _freed flag is checked without lock. In multi-threaded scenarios, use close() only after all polling threads have stopped to avoid race conditions.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If manager has been freed |
Source code in src/cymongoose/_mongoose.pyi
timer_add(milliseconds, callback, *, repeat=False, run_now=False)
¶
Add a timer that calls a Python callback periodically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
milliseconds
|
int
|
Timer interval in milliseconds |
required |
callback
|
Callable[[], None]
|
Python callable (takes no arguments) |
required |
repeat
|
bool
|
If True, timer repeats; if False, runs once |
False
|
run_now
|
bool
|
If True, callback is called immediately |
False
|
Returns:
| Type | Description |
|---|---|
Timer
|
Timer object (caller must keep a reference to it) |
Note: Timers are automatically freed when they complete (MG_TIMER_AUTODELETE flag). The Timer object's dealloc only releases the Python callback reference.
Warning: The returned Timer must be kept alive (e.g., stored in a variable) for as long as the timer is active. If the Timer object is garbage collected while Mongoose still holds the timer, the callback pointer becomes dangling.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If manager has been freed or timer creation failed |
Example
def heartbeat(): print("ping")
timer = manager.timer_add(1000, heartbeat, repeat=True)
Source code in src/cymongoose/_mongoose.pyi
run(poll_ms=100)
¶
Run the event loop, blocking until SIGINT or SIGTERM.
Installs signal handlers for graceful shutdown, runs the poll loop, and calls close() when done. Original signal handlers are restored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poll_ms
|
int
|
Poll timeout in milliseconds (default: 100) |
100
|
Source code in src/cymongoose/_mongoose.pyi
Overview¶
The Manager class is the core of cymongoose. It manages the Mongoose event loop and all network connections.
Creating a Manager¶
from cymongoose import Manager
# With default handler for all connections
def handler(conn, ev, data):
print(f"Event {ev} on connection {conn.id}")
manager = Manager(handler)
# Without default handler (use per-connection handlers)
manager = Manager()
# With wakeup support for multi-threading
manager = Manager(handler, enable_wakeup=True)
# With error handler for exceptions in event handlers
def on_error(exc):
print(f"Handler error: {exc}")
manager = Manager(handler, error_handler=on_error)
Constructor¶
cymongoose.Manager.__init__(handler=None, enable_wakeup=False, error_handler=None)
¶
Initialize event manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler
|
Optional[EventHandler]
|
Default event handler for all connections |
None
|
enable_wakeup
|
bool
|
Enable wakeup support for multi-threaded scenarios |
False
|
error_handler
|
Optional[Callable[[Exception], Any]]
|
Optional callback invoked with the exception when an event handler raises. Defaults to traceback.print_exc(). |
None
|
Source code in src/cymongoose/_mongoose.pyi
Listening for Connections¶
Create server sockets that accept incoming connections.
HTTP/HTTPS Server¶
# HTTP server (http= auto-inferred from scheme)
listener = manager.listen('http://0.0.0.0:8000')
# HTTPS server (requires TLS initialization)
listener = manager.listen('https://0.0.0.0:8443')
def handler(conn, ev, data):
if ev == MG_EV_ACCEPT and conn.is_tls:
# Initialize TLS on accepted connection
opts = TlsOpts(cert=cert, key=key)
conn.tls_init(opts)
TCP/UDP Server¶
# TCP server
tcp_listener = manager.listen('tcp://0.0.0.0:1234')
# UDP server
udp_listener = manager.listen('udp://0.0.0.0:5678')
MQTT Broker¶
Per-Listener Handler¶
Override the default handler for specific listeners:
def api_handler(conn, ev, data):
# Handle API requests
pass
def ws_handler(conn, ev, data):
# Handle WebSocket connections
pass
manager = Manager(default_handler)
manager.listen('http://0.0.0.0:8000', handler=api_handler)
manager.listen('http://0.0.0.0:9000', handler=ws_handler)
Methods¶
cymongoose.Manager.listen(url, handler=None, *, http=None)
¶
Listen on a URL; handler is optional per-listener override.
The http flag is inferred from the URL scheme when not provided
explicitly: http://, https://, ws://, wss:// all
enable HTTP protocol parsing. Pass http=False to override.
To listen on an OS-assigned port, pass port 0 and read the actual port from the returned connection::
listener = mgr.listen("http://0.0.0.0:0")
port = listener.local_addr[1]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to listen on (e.g., "http://0.0.0.0:8000", "tcp://0.0.0.0:1234") |
required |
handler
|
Optional[EventHandler]
|
Optional per-connection handler (overrides default) |
None
|
http
|
Optional[bool]
|
Enable HTTP protocol handler (inferred from scheme if None) |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Listener connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to listen on URL |
Source code in src/cymongoose/_mongoose.pyi
cymongoose.Manager.mqtt_listen(url, handler=None)
¶
Listen for MQTT connections (broker mode).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Listen URL (e.g., 'mqtt://0.0.0.0:1883') |
required |
handler
|
Optional[EventHandler]
|
Event handler callback |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Listener connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to listen for MQTT |
Source code in src/cymongoose/_mongoose.pyi
Making Connections¶
Create outbound client connections.
HTTP/HTTPS Client¶
def client_handler(conn, ev, data):
if ev == MG_EV_CONNECT:
# Send HTTP request
conn.send(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
elif ev == MG_EV_HTTP_MSG:
print(f"Status: {data.status()}")
print(f"Body: {data.body_text}")
conn.close()
# HTTP client (http= auto-inferred from scheme)
conn = manager.connect('http://example.com:80', client_handler)
# HTTPS client (TLS auto-initialized)
conn = manager.connect('https://example.com:443', client_handler)
WebSocket Client¶
def ws_handler(conn, ev, data):
if ev == MG_EV_WS_OPEN:
print("WebSocket connected")
conn.ws_send("Hello!", WEBSOCKET_OP_TEXT)
elif ev == MG_EV_WS_MSG:
print(f"Received: {data.text}")
# WebSocket client (handles upgrade automatically)
conn = manager.ws_connect('ws://example.com/ws', ws_handler)
MQTT Client¶
def mqtt_handler(conn, ev, data):
if ev == MG_EV_MQTT_OPEN:
print("Connected to broker")
conn.mqtt_sub("sensors/#", qos=1)
elif ev == MG_EV_MQTT_MSG:
print(f"{data.topic}: {data.text}")
conn = manager.mqtt_connect(
'mqtt://broker.hivemq.com:1883',
handler=mqtt_handler,
client_id='my-client',
clean_session=True,
keepalive=60,
)
SNTP Client¶
def sntp_handler(conn, ev, data):
if ev == MG_EV_SNTP_TIME:
# data is milliseconds since epoch
print(f"Time: {data} ms")
conn = manager.sntp_connect('udp://time.google.com:123', sntp_handler)
conn.sntp_request()
Methods¶
cymongoose.Manager.connect(url, handler=None, *, http=None)
¶
Create an outbound connection and return immediately.
The http flag is inferred from the URL scheme when not provided
explicitly: http://, https://, ws://, wss:// all
enable HTTP protocol parsing. Pass http=False to override.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to connect to (e.g., "http://example.com", "tcp://example.com:1234") |
required |
handler
|
Optional[EventHandler]
|
Optional per-connection handler (overrides default) |
None
|
http
|
Optional[bool]
|
Enable HTTP protocol handler (inferred from scheme if None) |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to connect to URL |
Source code in src/cymongoose/_mongoose.pyi
cymongoose.Manager.ws_connect(url, handler=None)
¶
Create an outbound WebSocket connection.
Connects and automatically sends the WebSocket upgrade handshake. The handler will receive MG_EV_WS_OPEN when the handshake completes, then MG_EV_WS_MSG for incoming frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
WebSocket URL (e.g., 'ws://example.com/ws', 'wss://example.com/ws') |
required |
handler
|
Optional[EventHandler]
|
Optional per-connection handler (overrides default) |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to connect |
Source code in src/cymongoose/_mongoose.pyi
cymongoose.Manager.mqtt_connect(url, handler=None, client_id='', username='', password='', clean_session=True, keepalive=60)
¶
Connect to an MQTT broker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Broker URL (e.g., 'mqtt://broker.hivemq.com:1883') |
required |
handler
|
Optional[EventHandler]
|
Event handler callback |
None
|
client_id
|
str
|
MQTT client ID (autogenerated if empty) |
''
|
username
|
str
|
MQTT username (optional) |
''
|
password
|
str
|
MQTT password (optional) |
''
|
clean_session
|
bool
|
Clean session flag |
True
|
keepalive
|
int
|
Keep-alive interval in seconds |
60
|
Returns:
| Type | Description |
|---|---|
Connection
|
Connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to connect to broker |
Source code in src/cymongoose/_mongoose.pyi
cymongoose.Manager.sntp_connect(url, handler=None)
¶
Connect to an SNTP (time) server.
Triggers MG_EV_SNTP_TIME event when time is received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
SNTP server URL (e.g., 'udp://time.google.com:123') |
required |
handler
|
Optional[EventHandler]
|
Event handler callback |
None
|
Returns:
| Type | Description |
|---|---|
Connection
|
Connection object |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If failed to connect to SNTP server |
Example
def time_handler(conn, ev, data): if ev == MG_EV_SNTP_TIME: # data is uint64_t epoch milliseconds print(f"Time: {data} ms since epoch")
conn = manager.sntp_connect("udp://time.google.com:123", time_handler) conn.sntp_request() # Request time
Source code in src/cymongoose/_mongoose.pyi
Event Loop¶
The event loop drives all I/O operations.
Simple Event Loop¶
The run() method handles signal setup, polling, and cleanup:
manager = Manager(handler)
manager.listen('http://0.0.0.0:8000')
manager.run() # Blocks until SIGINT/SIGTERM, then cleans up
Manual Polling¶
For more control, use poll() directly:
import signal
shutdown_requested = False
def signal_handler(sig, frame):
global shutdown_requested
shutdown_requested = True
signal.signal(signal.SIGINT, signal_handler)
manager = Manager(handler)
manager.listen('http://0.0.0.0:8000')
# Poll with 100ms timeout
while not shutdown_requested:
manager.poll(100)
manager.close()
Timeout Guidelines¶
- 100ms - Recommended default (responsive shutdown, low CPU)
- 0ms - Non-blocking (busy loop, 100% CPU)
- 1000ms+ - Longer timeouts (slow shutdown response)
See Performance for details.
Methods¶
cymongoose.Manager.run(poll_ms=100)
¶
Run the event loop, blocking until SIGINT or SIGTERM.
Installs signal handlers for graceful shutdown, runs the poll loop, and calls close() when done. Original signal handlers are restored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poll_ms
|
int
|
Poll timeout in milliseconds (default: 100) |
100
|
Source code in src/cymongoose/_mongoose.pyi
cymongoose.Manager.poll(timeout_ms=0)
¶
Drive the event loop once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout_ms
|
int
|
Timeout in milliseconds (0 = non-blocking) |
0
|
Thread safety note: _freed flag is checked without lock. In multi-threaded scenarios, use close() only after all polling threads have stopped to avoid race conditions.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If manager has been freed |
Source code in src/cymongoose/_mongoose.pyi
Active Connections¶
The connections property returns a snapshot of all active connections:
# Broadcast to all WebSocket clients
for conn in manager.connections:
if conn.is_websocket:
conn.ws_send("broadcast message", WEBSOCKET_OP_TEXT)
cymongoose.Manager.connections
property
¶
Return a snapshot of all active connections as a tuple.
Timers¶
Execute callbacks periodically.
One-Shot Timer¶
def callback():
print("Timer fired!")
# Fire once after 5 seconds
timer = manager.timer_add(5000, callback)
Repeating Timer¶
def heartbeat():
print(f"Alive at {time.time()}")
# Fire every second
timer = manager.timer_add(1000, heartbeat, repeat=True)
Immediate Execution¶
# Run immediately, then repeat every 1 second
timer = manager.timer_add(1000, callback, repeat=True, run_now=True)
Cancelling a Timer¶
Call timer.cancel() to stop a repeating timer early. Cancellation is
thread-safe -- it can be called from any thread, not just the poll thread.
The actual removal from the event loop is deferred to the next poll() call.
timer = manager.timer_add(1000, heartbeat, repeat=True)
# Later, from any thread:
timer.cancel()
print(timer.active) # False
Timer Lifecycle¶
- One-shot timers are automatically freed after firing (
MG_TIMER_AUTODELETEflag). - Repeating timers run until
cancel()is called ormanager.close()frees them. - The manager keeps an internal reference to all live timers, so discarding the
return value of
timer_add()is safe (the callback will not dangle).
Timer Properties¶
| Property | Type | Description |
|---|---|---|
active |
bool |
True if the timer has not been cancelled or completed |
Methods¶
cymongoose.Manager.timer_add(milliseconds, callback, *, repeat=False, run_now=False)
¶
Add a timer that calls a Python callback periodically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
milliseconds
|
int
|
Timer interval in milliseconds |
required |
callback
|
Callable[[], None]
|
Python callable (takes no arguments) |
required |
repeat
|
bool
|
If True, timer repeats; if False, runs once |
False
|
run_now
|
bool
|
If True, callback is called immediately |
False
|
Returns:
| Type | Description |
|---|---|
Timer
|
Timer object (caller must keep a reference to it) |
Note: Timers are automatically freed when they complete (MG_TIMER_AUTODELETE flag). The Timer object's dealloc only releases the Python callback reference.
Warning: The returned Timer must be kept alive (e.g., stored in a variable) for as long as the timer is active. If the Timer object is garbage collected while Mongoose still holds the timer, the callback pointer becomes dangling.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If manager has been freed or timer creation failed |
Example
def heartbeat(): print("ping")
timer = manager.timer_add(1000, heartbeat, repeat=True)
Source code in src/cymongoose/_mongoose.pyi
Multi-threading Support¶
The wakeup() method enables thread-safe communication with the event loop.
Setup¶
Enable wakeup support when creating the manager:
Background Worker Pattern¶
import threading
import queue
# Work queue
work_queue = queue.Queue()
result_queue = queue.Queue()
def worker():
"""Background worker thread."""
while True:
work = work_queue.get()
if work is None:
break
# Process work
result = expensive_computation(work['data'])
# Send result back via wakeup
result_queue.put({
'conn_id': work['conn_id'],
'result': result,
})
manager.wakeup(work['conn_id'], b"result_ready")
# Start worker thread
worker_thread = threading.Thread(target=worker, daemon=True)
worker_thread.start()
def handler(conn, ev, data):
if ev == MG_EV_HTTP_MSG:
# Offload to worker
work_queue.put({
'conn_id': conn.id,
'data': data.body_bytes,
})
elif ev == MG_EV_WAKEUP:
# Result ready
result = result_queue.get()
conn.reply(200, result['result'])
conn.drain()
See Threading for complete example.
Methods¶
cymongoose.Manager.wakeup(connection_id, data=b'')
¶
Send a wakeup notification to a specific connection (thread-safe).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id
|
int
|
The connection ID to wake up |
required |
data
|
bytes
|
Optional data payload (delivered via MG_EV_WAKEUP event) |
b''
|
Returns:
| Type | Description |
|---|---|
bool
|
True if wakeup was sent successfully |
Thread safety note: _freed flag is checked without lock. In multi-threaded scenarios, use close() only after all polling threads have stopped to avoid race conditions.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If manager has been freed |
Source code in src/cymongoose/_mongoose.pyi
Cleanup¶
Always clean up resources when done.