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.

AsyncToSync freezes threads for really long time on channel_layer.group_add

See original GitHub issue

Im trying to make the async call to group_add synchronous like in the docs … but when I do the whole request cycle freezes and doesn’t let me take other requests.

 39     def make_sub(info, gid):
 40         inst = relay.Node.get_node_from_global_id(info, gid)
 41         try:
 42             gp_name = 'gqp.{0}-updated.{1}'.format(str.lower(inst.__class__.__name__), inst.pk)
 43             # Group(gp_name).add(info.context.reply_channel)
 44             # info.context.channel_session['Groups'] = ','.join( (gp_name, info.context.channel_session['Groups']))
 45             __import__('pdb').set_trace()
 46  ->         AsyncToSync(channel_layer.group_add)(
 47                     gp_name,
 48                     info.context['reply_channel']
 49                 )
 50             if 'Groups' in info.context.scope['session']:
 51                 info.context.scope['session']['Groups'] = ','.join((gp_name, info.context.scope['session']['Groups']))
 52         except:
 53             pass
 54         return iter([inst])
(Pdb) channel_layer.group_add
<bound method RedisChannelLayer.group_add of <channels_redis.core.RedisChannelLayer object at 0x7fb322c835f8>>
(Pdb) gp_name
'gqp.product-updated.1'
(Pdb) info.context['reply_channel']
'specific..YgcGdjPn!HfWtmWwbyFou'
(Pdb) inst
<Product: atests>
(Pdb) AsyncToSync
<class 'asgiref.sync.AsyncToSync'>
(Pdb) c



Issue Analytics

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

github_iconTop GitHub Comments

1reaction
andrewgodwincommented, Feb 14, 2018

The key thing to remember is:

  • Never call a sync function from an async function without await sync_to_async(function)(args, ...)
  • Never call an async function from a sync function without async_to_sync(function)(args, ...)
0reactions
andrewgodwincommented, Feb 14, 2018

Oh, in the code example you posted. If you’re in an AsyncConsumer already then just convert all the functions to async functions (async def) then await them.

Or, if you want to keep those functions sync, use sync_to_async when you switch from an async def to calling a def, e.g. result = await sync_to_async(schema.execute)(self.query, ...)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Channels Error: you cannot use AsyncToSync in the ...
Exception inside application: You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly....
Read more >
Channels group_send hangs in extrenal process
I've never tried using the async-to-sync stuff with multiprocessing - first, I'd try removing that and see if it still works (just run...
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