Multiple threads
Thomas Rachel
nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Wed Nov 16 11:45:29 EST 2011
Am 16.11.2011 14:48 schrieb Eduardo Oliva:
> Hello, I have a py script that reads for all "m2ts" video files and convert them to "mpeg" using ffmpeg with command line.
>
> What I want to do is:
>
> I need my script to run 2 separated threads, and then when the first has finished, starts the next one....but no more than 2 threads.
> I know that Semaphores would help with that.
> But the problem here is to know when the thread has finished its job, to release the semaphore and start another thread.
>
> Any help would be great.
I'm not sure if you need threads at all: if you launch a process with
subprocess, it runs and you only would have to wait() for it. The same
can be done with two processes.
Pseudocode:
LIMIT = 2
processes = []
def do_waiting(limit):
while len(processes) >= limit:
% take the first one...
sp = processes.pop(0)
% wait for it...
st = sp.wait(100)
if is None:
% timeout, not finished yet, push back.
processes.append(sp)
else:
% finished - don't push back, let outer for loop continue.
print sp, "has finished with", st
for fname in list:
% launch process ...
sp = subprocess.Popen(...)
% ... and register it.
processes.append(sp)
% If we are on the limit, wait for process to finish.
do_waiting(LIMIT)
do_waiting(1)
Thomas
More information about the Python-list
mailing list