Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active August 29, 2015 14:05
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/0c5032846f4d31ab7160 to your computer and use it in GitHub Desktop.
Save stephenlb/0c5032846f4d31ab7160 to your computer and use it in GitHub Desktop.
Rapid Publishing of Messages on PubNub. You can publish message efficiently using this mechanism. How efficient you ask? This method allows you to publish upwards of 30 messages **Per TCP Packet!**. Wow. See https://gist.github.com/stephenlb/9496723 for more information.
import socket
import uuid
import random
import threading
import time
import random
import math
HOST = 'pubsub.pubnub.com'
PORT = 80
numID = random.randrange(5000, 10000)
print 'creating', numID, ' UUIDs'
id = []
for x in range(0, numID-1):
id.append(uuid.uuid4())
print 'We will send a random quantity of messages every 10 seconds, each contains a randomly selected UUID.'
def socketPool(message):
#TODO: Implement socketPool w/ size 100
def handleSocketRead(s):
while True:
try:
msg = s.recv(4096)
except socket.timeout, e:
err = e.args[0]
print 'recv timed out, done reading'
break
except socket.error, e:
print e
break
else:
continue
#print repr(msg)
def send():
time.sleep(10)
threading.Thread(target=send).start()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostbyname(HOST), PORT))
s.settimeout(2)
numMessages = random.randrange(math.floor(0.5*numID), 2*numID)
print 'Sending '+ str(numMessages) +' messages.'
time_start = time.time()
tmr = threading.Thread(target=handleSocketRead, args=[s])
tmr.start()
for message in range(0, numMessages):
#print 'we are on message', message+1, 'of', numMessages
s.send('GET /publish/pub-c-5afaf11d-aa91-4a40-b0d2-77961fb3a258/sub-c-0cd3a376-28ac-11e4-95a7-02ee2ddab7fe/0/HyperLogLogDemo1/0/"'+str(id[random.randrange(0, numID-1)])+'" HTTP/1.1\r\nHost: pubsub.pubnub.com\r\n\r\n')
print 'It took', str(time.time()-time_start), 'runtime to send', numMessages, 'messages'
handleSocketRead(s)
s.close()
## Send Messages Quickly
if __name__ == "__main__":
send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment