AsyncToSync freezes threads for really long time on channel_layer.group_add
See original GitHub issueIm 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:
- Created 6 years ago
- Comments:10 (10 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The key thing to remember is:
await sync_to_async(function)(args, ...)
async_to_sync(function)(args, ...)
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
) thenawait
them.Or, if you want to keep those functions sync, use
sync_to_async
when you switch from anasync def
to calling adef
, e.g.result = await sync_to_async(schema.execute)(self.query, ...)