aboutsummaryrefslogtreecommitdiffstats
path: root/test.py
blob: 6a4007b57b4987404dc600a9a5d37acd72747993 (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
#!/usr/bin/env python3


import contextlib
import os
import random
import socket
import subprocess


procs = []
procmax = 2
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
with contextlib.suppress(FileNotFoundError):
    os.unlink('socket')
s.bind(b'socket')
s.listen(0)
while True:
    client, _ = s.accept()
    line = client.recv(1024)
    if not line.endswith(b'\n'):
        client.close()
        continue
    filename = line.rstrip()
    client.close()

    print('running for filename {}'.format(filename))

    p = subprocess.Popen("sleep {} && echo ffmpeg {}"
            .format(random.randint(1, 10), filename), shell=True)

    procs.append(p)

    if len(procs) >= procmax:
        os.wait()
        procs = list(p for p in procs if p.poll() is not None)