[Tutor] Newbie Wondering About Threads

Kent Johnson kent37 at tds.net
Sun Dec 7 16:47:53 CET 2008


On Sun, Dec 7, 2008 at 8:58 AM, Damon Timm <damontimm at gmail.com> wrote:
> On Sun, Dec 7, 2008 at 12:33 AM, Martin Walsh <mwalsh at mwalsh.org> wrote:

>> Here is my simplistic, not-very-well-thought-out, attempt in
>> pseudo-code, perhaps it will get you started ...
>>
>> paths = ["file1.flac","file2.flac", ... "file11.flac"]
>> procs = []
>> while paths or procs:
>>    procs = [p for p in procs if p.poll() is None]
>>    while paths and len(procs) < 2:
>>        flac = paths.pop(0)
>>        procs.append(Popen(['...', flac], ...))
>>    time.sleep(1)
>
> I think I got a little lost with the "procs = [p for p in procs if
> p.poll() is None]" statement

It's called a list comprehension
http://personalpages.tds.net/~kent37/kk/00003.html

Essentially it creates a new list from all the elements it the old
list that are still running.

> On Sun, Dec 7, 2008 at 2:58 AM, Lie Ryan <lie.1296 at gmail.com> wrote:

> Yea, looks like it - I think the trick, for me, will be getting a
> dynamic list that can be iterated through ... I experimented a little
> with the .poll() function and I think I follow how it is working ...
> but really, I am going to have to do a little more "pre-thinking" than
> I had to do with the bash version ... not sure if I should create a
> class containing the list of flac files or just a number of functions
> to handle the list ... whatever way it ends up being, is going to take
> a little thought to get it straightened out.  And the objected
> oriented part is different than bash -- so, I have to "think
> different" too.

I don't think you need any classes for this. A simple list of file
names should be fine. A function that takes a file name as a
parameter, starts a process to process the file, and returns the
resulting Popen object would also be helpful.

> On Sun, Dec 7, 2008 at 8:31 AM, Kent Johnson <kent37 at tds.net> wrote:

> Oh neat!  I will be honest, more than one screen full of code and I
> get a little overwhelmed (at this point) but I am going to check that
> idea out.  I was thinking something along these lines, where I can
> send all the input/ouput variables along with a number argument
> (threads) to a class/function that would then handle everything ... so
> using a thread pool may make sense ...
>
> Looks like I would create a loop that went through the list of all the
> files to be converted and then sent them all off, one by one, to the
> thread pool -- which would then just dish them out so that no more
> than 2 (if I chose that) would be converting at a time?

Yes, that's right.

>  I gotta try
> and wrap my head around it ... also, I will be using two subprocesses
> to accomplish a single command (one for stdoutput and the other taking
> stdinput) as well ... so they have to be packaged together somehow ...

A function as mentioned above would help. For the threaded solution
the function could just start the child process and wait for it to
finish, it doesn't have to return anything. Each thread will block on
its associated child.

Kent


More information about the Tutor mailing list