question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[channels 2] Send to a single channel from sync code

See original GitHub issue

channel_layer.send seems to be an async function. So how would I call it from sync code? Should asgiref.sync.async_to_sync be used in such case? Also docs don’t mention it being async.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
andriilahutacommented, Feb 3, 2018

I’ve got another problem. Wrapping with AsyncToSync works only if we are somewhere in asgi app. If I call wrapped function from another process (management command for example) then it fails with Cannot call async functions without an event loop running. If I call it from another process which has its own asyncio loop then it just hangs.

As example, I have the following function:

def send_email(recipient, subject, text):
    channel_layer = get_channel_layer()
    AsyncToSync(channel_layer.send)(
        'email.dispatcher',
        dict(type='dispatch', email=dict(recipient=recipient, subject=subject, text=text))
    )

I’d like it to be multipurpose, and avoid writing 3 different versions of this function. Can something be done about this?

2reactions
andrewgodwincommented, Feb 3, 2018

I’ll try and clean that up if I can replicate the environment it happens in (I already fixed one hanging issue yesterday, but it makes my brain hurt a bit so I need a short break)

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's new in Channels 2? — Channels 2.4.0 documentation
Channels 1 and Channels 2 are substantially different codebases, ... only jumping to synchronous code when you interact with the Django view system...
Read more >
std::sync::mpsc - Rust
These channels come in two flavors: An asynchronous, infinitely buffered channel. The channel function will return a (Sender, Receiver) tuple where all sends...
Read more >
Sending different types using same Rust channel (mpsc)
One particular channel can only send one type of data. As you noticed, your sender has the type Sender<String> and the receiver has...
Read more >
Module mpsc - tokio::sync - Docs.rs
A multi-producer, single-consumer queue for sending values between asynchronous tasks. This module provides two variants of the channel: bounded and ...
Read more >
Channels | Tokio - An asynchronous Rust runtime
We could use tokio::sync::Mutex , but that would only allow a single in-flight ... Now, update the two tasks to send commands over...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found