Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/tests/test_callback_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def test_callback_wait_method(nats: Nats) -> None:
limit = 10

async def callback(msg: Message) -> None:
await asyncio.sleep(0.1)
received.append(msg.payload)

async with nats.subscribe(subject=subj, callback=callback) as sub:
Expand Down
8 changes: 7 additions & 1 deletion src/subscriptions/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ async fn start_py_sub(
mut unsub_receiver: tokio::sync::mpsc::Receiver<UnsubscribeCommand>,
end_event: AsyncEvent,
) {
// Required to wait for completion of processing tasks.
//
// This will ensure that end_event is only set after
// processing of all messages is finished.
let mut tasks = tokio::task::JoinSet::new();
loop {
tokio::select! {
msg = sub.next() => {
match msg {
Some(message) => {
let py_cb = py_callback.clone();
tokio::spawn(pyo3_async_runtimes::tokio::scope(
tasks.spawn(pyo3_async_runtimes::tokio::scope(
locals.clone(),
process_message(message, py_cb),
));
Expand All @@ -77,6 +82,7 @@ async fn start_py_sub(
}
}
}
tasks.join_all().await;
end_event.set();
}

Expand Down
Loading