Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Add a sync send_nowait() to BroadcastSender #600

Open

Description

@llucax

What's needed?

Sync code can't call the async send(), as the data sourcing and resampler use cases in #76 show. A BroadcastSender never waits for a receiver to have room: a full receiver drops its oldest message instead. So sending doesn't need to be async.

Proposed solution

Add a sync send_nowait(message) method to BroadcastSender. The name follows the standard library: asyncio.Queue.put_nowait() is the sync version of put(). trio's MemorySendChannel.send_nowait() and anyio's MemoryObjectSendStream.send_nowait() work the same way.

def send_nowait(self, message: _T, /) -> None: ...

send_nowait() isn't added to the abstract Sender. Not every sender can send without waiting, for example one that sends over a network. trio's and anyio's abstract send channels don't have it either. An abstract interface for senders that have send_nowait() can come later with #76.

If broadcast channels get senders that can wait (#602), those could get a send_nowait() that raises an error instead of waiting.

This targets v2. It isn't a breaking change, so any v2 minor release can add it. The implementation is simple in v2: a broadcast sender wakes up its receivers by setting an asyncio.Event, which works from sync code. In v1, a broadcast sender notifies an asyncio.Condition. That needs the condition's lock, and a sync method can't wait to acquire it.

Alternatives and workarounds

Send from a task

Sync code running in the event loop's thread can call asyncio.create_task(sender.send(message)).

  • Pros: it works today, without any change.
  • Cons: it creates a task for every message. The message is only sent after the sync code returns to the event loop. Errors are lost unless something awaits the task.

Activity

  1. added this to the v2.0.0 milestone on Nov 12, 2026
  2. added
    type:enhancement
    part:channels
    on Nov 12, 2026
  3. modified the milestones: v2.0.0, Untriaged on Nov 12, 2026
  4. changed the title [-]Add a sync `send_nowait()` to broadcast senders[/-] [+]Add a sync `send_nowait()` to `BroadcastSender`[/+] on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    part:channelstype:enhancement

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions