Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
70 lines (45 sloc) 1.61 KB
import urllib2 as u
import json
import time
import threading as t
import random
def get_data(post_id,data):
def __get_data():
site= "" + str(post_id)
hdr = {'User-Agent': 'Mozilla/5.0'}
req = u.Request(site,headers=hdr)
raw_data = u.urlopen(req).read()
""" This is to simulate delays different calls may induce as network latency isint the same for all """
print "data received -- " + str(post_id)
""" Setting the flag and populating the data at the same time """
data[post_id] = json.loads(raw_data)['title']
my_thread = t.Thread(target=__get_data)
def handle_data_1():
print "1 has been handled"
def handle_data_2():
print "2 has been handled"
def handle_data_3():
print "3 has been handled"
def boss_thread():
""" We use data dict as a collection of flags as it is shared among the threads """
data = {}
current = 0
while True:
if current < 3:
current += 1
get_data(current, data)
""" These are the handlers for the data retreived in the worker threads"""
if 1 in data and data[1]:
data[1] = False
if 2 in data and data[2]:
data[2] = False
if 3 in data and data[3]:
data[3] = False
print "processing"