forked from 361way/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_thread1.py
More file actions
41 lines (35 loc) · 1.08 KB
/
queue_thread1.py
File metadata and controls
41 lines (35 loc) · 1.08 KB
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
#!/usr/bin/env python
## DATE: 2011-01-20
## FILE: queue.py
## WEBSITE: http://themattreid.com
from Queue import *
from threading import Thread, Lock
'''this function will process the items in the queue, in serial'''
def processor():
if queue.empty() == True:
print "the Queue is empty!"
sys.exit(1)
try:
job = queue.get()
print "I'm operating on job item: %s"%(job)
queue.task_done()
except:
print "Failed to operate on job"
'''set variables'''
queue = Queue()
threads = 4
'''a list of job items. you would want this to be more advanced,
like reading from a file or database'''
jobs = [ "job1", "job2", "job3" ]
'''iterate over jobs and put each into the queue in sequence'''
#for job in jobs:
for job in range(100):
print "inserting job into the queue: %s"%(job)
queue.put(job)
'''start some threads, each one will process one job from the queue'''
for i in range(threads):
th = Thread(target=processor)
th.setDaemon(True)
th.start()
'''wait until all jobs are processed before quitting'''
queue.join()