-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread_no_callbacks.py
69 lines (45 loc) · 1.61 KB
/
thread_no_callbacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import urllib2 as u
import json
import time
import threading as t
import random
def get_data(post_id,data):
def __get_data():
site= "http://jsonplaceholder.typicode.com/posts/" + 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 """
time.sleep(random.randint(1,10))
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)
my_thread.start()
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)
time.sleep(2)
""" These are the handlers for the data retreived in the worker threads"""
if 1 in data and data[1]:
handle_data_1()
data[1] = False
if 2 in data and data[2]:
handle_data_2()
data[2] = False
if 3 in data and data[3]:
handle_data_3()
data[3] = False
print "processing"
boss_thread()