summaryrefslogtreecommitdiffstats
path: root/tests/wqueue.py
blob: 528a10c8b78ed78f64004f0b763baaad6a62c7e5 (plain)
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
59
60
61
62
63
#!/usr/bin/python3

import fcntl
import time
import subprocess
import os
import asyncio

QUEUE_FILE = 'queue.txt'
COMMAND = './command.sh'

loop = asyncio.get_event_loop()

class JobHandler(asyncio.SubprocessProtocol):
    def process_exited(self):
        print()

def do_job(command=None, arg=None):
    assert(command)
    assert(arg)
    print('arg:', arg)
    print('doing operation with arg')
    #ret = subprocess.check_call([command, arg])
    loop.suprocess_exec([command, arg],
            stdin=subprocess.DEVNULL,
            stdout=sys.stdout,
            stderr=sys.stderr)
    print('ret:', ret)
    return ret == 0

def read_next_job_arg():
    with open(QUEUE_FILE, 'r') as f:
        line = f.readline()
    return line.strip()

def pop_job_arg():
    queue = QUEUE_FILE
    queue_tmp = queue + '.tmp'
    linecount = 0
    with open(queue, 'r') as fi, open(queue_tmp, 'w') as fo:
        for line in fi:
            linecount += 1
            if linecount == 1:
                continue
            fo.write(line)
    os.rename(queue_tmp, queue)

def check_queue():
    #loop = asyncio.get_event_loop()
    #loop.run_forever()


#with open('queue.txt', 'w') as f:
#    fcntl.lockf(f, fcntl.LOCK_EX)
#    time.sleep(9999)

#while True:
#    job_arg = read_next_job_arg()
#    if do_job(command=COMMAND, arg=job_arg):
#        pop_job_arg()

#loop = asyncio.get_event_loop()
loop.run_forever()