Skip to content

Instantly share code, notes, and snippets.

@pixyj
Created April 18, 2016 10:12
Show Gist options
  • Save pixyj/c91237c569a1b7312b953cc0963eb923 to your computer and use it in GitHub Desktop.
Save pixyj/c91237c569a1b7312b953cc0963eb923 to your computer and use it in GitHub Desktop.
import Queue
import threading
# This is the current version of the database.
rows = {
1: {
"version": 2,
"postcode": 111111,
"id": 1
},
2: {
"version": 8,
"postcode": 222222,
"id": 2
},
3: {
"version": 10,
"postcode": 333333,
"id": 3
},
4: {
"version": 5,
"postcode": 444444,
"id": 4
}
}
class UpdateDataThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
print "waiting for message"
client_data = self.queue.get()
pk = client_data['id']
db_data = rows[pk]
if client_data['version'] < db_data['version']:
print 'Ignoring ', client_data
continue
else:
print 'Saving ', client_data
rows[pk] = client_data
def run():
# We have 2 threads in the thread pool. Primary keys 1 and 3 will be mapped to first thread
# Primary keys 2 and 4 will be mapped to the second thread
THREAD_POOL_SIZE = 2
threads = []
queues = []
for i in range(THREAD_POOL_SIZE):
queue = Queue.Queue()
thread = UpdateDataThread(queue)
threads.append(thread)
queues.append(queue)
thread.start()
#Now threads are ready to process messages
#Start sending requests
requests = {
1: {
"version": 5,
"postcode": 123456,
"id": 1
},
2: {
"version": 4,
"postcode": 222,
"id": 2
},
3: {
"version": 2,
"postcode": 33,
"id": 3
},
4: {
"version": 10,
"postcode": 123456,
"id": 4
}
}
for key, value in requests.items():
queue = queues[key % THREAD_POOL_SIZE]
queue.put(value)
# Keep the main thread alive.
for thread in threads:
thread.join()
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment