-
Notifications
You must be signed in to change notification settings - Fork 0
/
quemgr.py
58 lines (47 loc) · 1.56 KB
/
quemgr.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Queue
import threading
import time
class QueMgr:
def __init__(self, db):
"""
The queue manager class, tends to solve the cocurrency problem,
when writing to the sqlite database. It happens when two or more
threads of a python application start to write to the sqlite files.
It gives the locked error.
here there is a thread runing that checks the database queue for items
and commands needed to added to the database.
"""
self.db = db
self.queue = Queue.Queue()
def insert(self, item, insert_type):
"""
Insert to the database, given item with given function.
Take a look at the database.py, there are two functions that write
in the database. So these are chosen.
Args:
item (dict or list)
insert_type string
Kwargs:
None
Returns:
None
"""
if(insert_type == "initState"):
self.db.update_all(item)
if(insert_type == "finalState"):
self.db.insert_final_state(item)
class QueMgrThread(threading.Thread):
def __init__(self, quemgr):
threading.Thread.__init__(self)
self.quemgr = quemgr
def run(self):
while(True):
if(self.quemgr.queue.empty()):
print "empty"
else:
print "hi"
self.quemgr.insert(item["item"], item["type"])
q.task_done()
time.sleep(5)