Skip to content

Commit f671ff3

Browse files
committed
first commit
0 parents  commit f671ff3

File tree

7 files changed

+1112
-0
lines changed

7 files changed

+1112
-0
lines changed

README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
threads: Python threads synchronization: Locks, RLocks, Semaphores, Conditions, Events and Queues.

threads/condition.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import time
2+
import urllib2
3+
import threading
4+
import random
5+
6+
class Producer(threading.Thread):
7+
"""
8+
Produces random integers to a list
9+
"""
10+
11+
def __init__(self, integers, condition):
12+
"""
13+
Constructor.
14+
15+
@param integers list of integers
16+
@param condition condition synchronization object
17+
"""
18+
threading.Thread.__init__(self)
19+
self.integers = integers
20+
self.condition = condition
21+
22+
def run(self):
23+
"""
24+
Thread run method. Append random integers to the integers list at random time.
25+
"""
26+
for i in range(10):
27+
integer = random.randint(0, 256)
28+
self.condition.acquire()
29+
print 'condition acquired by %s' % self.name
30+
self.integers.append(integer)
31+
print '%d appended to list by %s' % (integer, self.name)
32+
print 'condition notified by %s' % self.name
33+
self.condition.notify()
34+
print 'condition released by %s' % self.name
35+
self.condition.release()
36+
time.sleep(1)
37+
38+
class Consumer(threading.Thread):
39+
"""
40+
Consumes random integers from a list
41+
"""
42+
43+
def __init__(self, integers, condition):
44+
"""
45+
Constructor.
46+
47+
@param integers list of integers
48+
@param condition condition synchronization object
49+
"""
50+
threading.Thread.__init__(self)
51+
self.integers = integers
52+
self.condition = condition
53+
54+
def run(self):
55+
"""
56+
Thread run method. Consumes integers from list
57+
"""
58+
while True:
59+
self.condition.acquire()
60+
print 'condition acquired by %s' % self.name
61+
while True:
62+
if self.integers:
63+
integer = self.integers.pop()
64+
print '%d popped from list by %s' % (integer, self.name)
65+
break
66+
print 'condition wait by %s' % self.name
67+
self.condition.wait()
68+
print 'condition released by %s' % self.name
69+
self.condition.release()
70+
71+
def main():
72+
integers = []
73+
condition = threading.Condition()
74+
t1 = Producer(integers, condition)
75+
t2 = Consumer(integers, condition)
76+
t1.start()
77+
t2.start()
78+
t1.join()
79+
t2.join()
80+
81+
if __name__ == '__main__':
82+
main()
83+

threads/event.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import time
2+
import urllib2
3+
import threading
4+
import random
5+
6+
class Producer(threading.Thread):
7+
"""
8+
Produces random integers to a list
9+
"""
10+
11+
def __init__(self, integers, event):
12+
"""
13+
Constructor.
14+
15+
@param integers list of integers
16+
@param event event synchronization object
17+
"""
18+
threading.Thread.__init__(self)
19+
self.integers = integers
20+
self.event = event
21+
22+
def run(self):
23+
"""
24+
Thread run method. Append random integers to the integers list at random time.
25+
"""
26+
for i in range(10):
27+
integer = random.randint(0, 256)
28+
self.integers.append(integer)
29+
print '%d appended to list by %s' % (integer, self.name)
30+
print 'event set by %s' % self.name
31+
self.event.set()
32+
print 'event cleared by %s' % self.name
33+
self.event.clear()
34+
time.sleep(1)
35+
36+
class Consumer(threading.Thread):
37+
"""
38+
Consumes random integers from a list
39+
"""
40+
41+
def __init__(self, integers, event):
42+
"""
43+
Constructor.
44+
45+
@param integers list of integers
46+
@param event event synchronization object
47+
"""
48+
threading.Thread.__init__(self)
49+
self.integers = integers
50+
self.event = event
51+
52+
def run(self):
53+
"""
54+
Thread run method. Consumes integers from list
55+
"""
56+
while True:
57+
self.event.wait()
58+
integer = self.integers.pop()
59+
print '%d popped from list by %s' % (integer, self.name)
60+
61+
def main():
62+
integers = []
63+
event = threading.Event()
64+
t1 = Producer(integers, event)
65+
t2 = Consumer(integers, event)
66+
t1.start()
67+
t2.start()
68+
t1.join()
69+
t2.join()
70+
71+
if __name__ == '__main__':
72+
main()
73+

threads/lock.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import time
2+
import urllib2
3+
import threading
4+
5+
class FetchUrls(threading.Thread):
6+
"""
7+
Thread checking URLs.
8+
"""
9+
10+
def __init__(self, urls, output, lock):
11+
"""
12+
Constructor.
13+
14+
@param urls list of urls to check
15+
@param output file to write urls output
16+
"""
17+
threading.Thread.__init__(self)
18+
self.urls = urls
19+
self.output = output
20+
self.lock = lock
21+
22+
def run(self):
23+
"""
24+
Thread run method. Check URLs one by one.
25+
"""
26+
while self.urls:
27+
url = self.urls.pop()
28+
req = urllib2.Request(url)
29+
try:
30+
d = urllib2.urlopen(req)
31+
except urllib2.URLError, e:
32+
print 'URL %s failed: %s' % (url, e.reason)
33+
self.lock.acquire()
34+
print 'lock acquired by %s' % self.name
35+
self.output.write(d.read())
36+
print 'write done by %s' % self.name
37+
print 'lock released by %s' % self.name
38+
self.lock.release()
39+
print 'URL %s fetched by %s' % (url, self.name)
40+
41+
def main():
42+
# list 1 of urls to fetch
43+
urls1 = ['http://www.google.com', 'http://www.facebook.com']
44+
# list 2 of urls to fetch
45+
urls2 = ['http://www.yahoo.com', 'http://www.youtube.com']
46+
lock = threading.Lock()
47+
f = open('output.txt', 'w+')
48+
t1 = FetchUrls(urls1, f, lock)
49+
t2 = FetchUrls(urls2, f, lock)
50+
t1.start()
51+
t2.start()
52+
t1.join()
53+
t2.join()
54+
f.close()
55+
56+
if __name__ == '__main__':
57+
main()
58+

threads/queue.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import time
2+
import threading
3+
import random
4+
import Queue
5+
6+
class Producer(threading.Thread):
7+
"""
8+
Produces random integers to a list
9+
"""
10+
11+
def __init__(self, queue):
12+
"""
13+
Constructor.
14+
15+
@param integers list of integers
16+
@param queue queue synchronization object
17+
"""
18+
threading.Thread.__init__(self)
19+
self.queue = queue
20+
21+
def run(self):
22+
"""
23+
Thread run method. Append random integers to the integers list at random time.
24+
"""
25+
while True:
26+
integer = random.randint(0, 256)
27+
self.queue.put(integer)
28+
print '%d put to queue by %s' % (integer, self.name)
29+
time.sleep(1)
30+
31+
class Consumer(threading.Thread):
32+
"""
33+
Consumes random integers from a list
34+
"""
35+
36+
def __init__(self, queue):
37+
"""
38+
Constructor.
39+
40+
@param integers list of integers
41+
@param queue queue synchronization object
42+
"""
43+
threading.Thread.__init__(self)
44+
self.queue = queue
45+
46+
def run(self):
47+
"""
48+
Thread run method. Consumes integers from list
49+
"""
50+
while True:
51+
integer = self.queue.get()
52+
print '%d popped from list by %s' % (integer, self.name)
53+
self.queue.task_done()
54+
55+
def main():
56+
integers = []
57+
queue = Queue.Queue()
58+
t1 = Producer(queue)
59+
t2 = Consumer(queue)
60+
t1.start()
61+
t2.start()
62+
t1.join()
63+
t2.join()
64+
65+
if __name__ == '__main__':
66+
main()
67+

threads/rlock.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import time
2+
import urllib2
3+
import threading
4+
5+
class FetchUrls(threading.Thread):
6+
"""
7+
Thread checking URLs.
8+
"""
9+
10+
def __init__(self, urls, output, lock):
11+
"""
12+
Constructor.
13+
14+
@param urls list of urls to check
15+
@param output file to write urls output
16+
"""
17+
threading.Thread.__init__(self)
18+
self.urls = urls
19+
self.output = output
20+
self.lock = lock
21+
22+
def run(self):
23+
"""
24+
Thread run method. Check URLs one by one.
25+
"""
26+
while self.urls:
27+
url = self.urls.pop()
28+
req = urllib2.Request(url)
29+
try:
30+
d = urllib2.urlopen(req)
31+
except urllib2.URLError, e:
32+
print 'URL %s failed: %s' % (url, e.reason)
33+
self.lock.acquire()
34+
print 'lock acquired by %s' % self.name
35+
self.output.write(d.read())
36+
print 'write done by %s' % self.name
37+
print 'lock released by %s' % self.name
38+
self.lock.release()
39+
print 'URL %s fetched by %s' % (url, self.name)
40+
41+
def main():
42+
# list 1 of urls to fetch
43+
urls1 = ['http://www.google.com', 'http://www.facebook.com']
44+
# list 2 of urls to fetch
45+
urls2 = ['http://www.yahoo.com', 'http://www.youtube.com']
46+
lock = threading.Lock()
47+
f = open('output.txt', 'w+')
48+
t1 = FetchUrls(urls1, f, lock)
49+
t2 = FetchUrls(urls2, f, lock)
50+
t1.start()
51+
t2.start()
52+
t1.join()
53+
t2.join()
54+
f.close()
55+
56+
if __name__ == '__main__':
57+
main()
58+

0 commit comments

Comments
 (0)