diff options
-rwxr-xr-x | tests/wqueue2.py | 26 |
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: |