Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Created August 8, 2018 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenlb/af711ec727def9930c55c0c2ff5164f2 to your computer and use it in GitHub Desktop.
Save stephenlb/af711ec727def9930c55c0c2ff5164f2 to your computer and use it in GitHub Desktop.
Python Subscribe: Basic Worker. Super Simple
import multiprocessing
import requests
SUB_KEY = 'demo'
CHANNELS = ['my_channel']
def main():
mp = multiprocessing.Process(target=subscriber)
mp.start()
mp.join()
def subscriber():
timetoken = '0' ## pointer to last message received
while True:
url = "/".join([
'https://ps.pubnub.com/subscribe'
, SUB_KEY
, ",".join(CHANNELS)
, '0'
, timetoken
])
print(url)
response = requests.get(url)
data = response.json()
messages = data[0]
timetoken = data[1]
print(data)
if __name__ == '__main__': main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment