Skip to content

Instantly share code, notes, and snippets.

@johngrantuk
Created March 29, 2016 18:20
Show Gist options
  • Save johngrantuk/c9a94e70fcdcb7c306a0748841aa0b5a to your computer and use it in GitHub Desktop.
Save johngrantuk/c9a94e70fcdcb7c306a0748841aa0b5a to your computer and use it in GitHub Desktop.
Django Channels background worker
from channels import Group
from django.core.management import BaseCommand
import time
#The class must be named Command, and subclass BaseCommand
class Command(BaseCommand):
# Show this when the user types help
help = "Simulates reading sensor and sending over Channel."
# A command must define handle()
def handle(self, *args, **options):
x = 0
while True:
Group("sensor").send({'text': "Sensor reading=" + str(x)})
time.sleep(2)
x = x + 1
self.stdout.write("Sensor reading..." + str(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment