Friday, February 23, 2018

Channels worker and call_later

TLDR: Is there a way to use the call_later function within a worker task? (Or am I just looking at this issue the wrong way?)

Software - Python 3.6, Django 2.0.2, Channels 2.0.2, trying to run this on either / both Windows 10 and Ubuntu 17.10 if it matters

Longer version - I'm having surprisingly few problems with wrapping my head around the new Channels 2, especially since this is the first I've been using asyncio and it's been about 10 years since I've used Twisted. All my personal sample code that I've been trying to write has been working great.

Except... I have the situation where I want to schedule an event for some fixed period of time in the future - say 30 seconds.
But, it's also necessary for me to be able to cancel that scheduled event.

I've tried a number of different combinations for the worker task between SyncConsumer and AsyncConsumer, and different combinations of async and regular methods, and get a variety of different errors. But the bottom line is that I haven't stumbled across the magic sauce that will let this work.

I _can_ get the delay to work by using the asyncio.sleep method, using the pattern shown in the "Delay server" paragraph in the "What's new in Channels 2?" section of the docs. But while that method (sleep) returns a generator object, I don't see where that does me any good with trying to cancel that sleep.

I know I can make this work by setting some variable, and check that after the sleep has finished to determine whether or not I still need to do my work, but that just "feels wrong" to me for some reason.

Is there something simple that I'm missing? Or does this fit into the category of "Don't do that!"

Thanks,
     Ken

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/642da2f0-5c07-44ee-9059-a366ef67de14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment