summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVG <vg@devys.org>2016-04-08 00:18:18 +0200
committerVG <vg@devys.org>2016-04-08 00:18:18 +0200
commit3d2835e1d90d4d9615f5c266943a49f1de8f619f (patch)
tree24c94bc70aa09099350e7b26a0514e068eacd451
parent37ba4be062c037616fc8a19057090d236422d9c0 (diff)
downloadwqueue-3d2835e1d90d4d9615f5c266943a49f1de8f619f.tar.gz
wqueue-3d2835e1d90d4d9615f5c266943a49f1de8f619f.tar.bz2
wqueue-3d2835e1d90d4d9615f5c266943a49f1de8f619f.zip
Auto-commit on 807fe7afb37fe2cfcf15c9457d04f64fa11b7511
-rwxr-xr-xtests/wqueue2.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/tests/wqueue2.py b/tests/wqueue2.py
index 087e345..5340d0f 100755
--- a/tests/wqueue2.py
+++ b/tests/wqueue2.py
@@ -13,13 +13,14 @@ import collections
u8open = functools.partial(open, encoding='utf8')
-class PersistentJobQueue2:
+class PersistentQueue3:
- def __init__(self, filename, loop=None):
+ def __init__(self, filename=None, loop=None):
self.queue = collections.deque()
self.current_job = collections.deque()
self.event = asyncio.Event(loop=loop)
self.loop = loop
+ self.filename = filename
def push_job(self, item):
self.queue.append(item)
@@ -41,7 +42,7 @@ class PersistentJobQueue2:
assert len(self.current_job)
self.current_job.pop()
- def dump_queue(self, filename):
+ def dump_queue(self, filename=None):
'''
dump and clean the queue in order for it to be edited.
@@ -56,25 +57,29 @@ class PersistentJobQueue2:
The view here is we do not want to redo the work on a file, if the
wanted result is a success: do not close the queue.
'''
+ filename = filename if filename else self.filename
with u8open(filename, 'w') as f:
for item in self.queue:
print(item, file=f)
self.queue.clear()
- def reload_from_filename(self, filename):
+ def reload_from_filename(self, filename=None):
'''
reloads a queue after editing it externaly.
'''
+ filename = filename if filename else self.filename
with u8open(filename) as f:
for item in f:
self.queue.append(item)
self.event.set()
-async def test():
- print('sleeping...')
- await asyncio.sleep(3)
- print('slept')
+async def do_the_job(job=None):
+ assert job
+ print('='*50)
+ print('Current job:', item)
+ do_the_job_here with await
+ print('Current job is done.')
async def manage_client(reader, writer):
@@ -94,10 +99,11 @@ async def manage_client(reader, writer):
async def manage_jobs():
- pq = PersistentQueue('queue.txt')
+ jobs = PersistentQueue3(filename='queue.txt')
queue = []
while True:
- item = await pq.pop()
+ job = await jobs.get_job()
+ do_the_job(job=job)
async with open('queue.txt') as f:
line = await next(f)
if not line: