[Tutor] Tutor Digest, Vol 58, Issue 22
the New me
newgat11 at yahoo.com
Sun Dec 7 18:58:17 CET 2008
That worked,
I tried it with string elements also
Thanks a bunch
--- On Sun, 12/7/08, tutor-request at python.org <tutor-request at python.org> wrote:
> From: tutor-request at python.org <tutor-request at python.org>
> Subject: Tutor Digest, Vol 58, Issue 22
> To: tutor at python.org
> Date: Sunday, December 7, 2008, 6:00 AM
> Send Tutor mailing list submissions to
> tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body
> 'help' to
> tutor-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more
> specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
> 1. Re: Newbie Wondering About Threads (Lie Ryan)
> 2. Re: Sorting on different fields (Lie Ryan)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sun, 7 Dec 2008 07:58:11 +0000 (UTC)
> From: Lie Ryan <lie.1296 at gmail.com>
> Subject: Re: [Tutor] Newbie Wondering About Threads
> To: tutor at python.org
> Message-ID: <ghfvmh$8b5$1 at ger.gmane.org>
> Content-Type: text/plain; charset=UTF-8
>
> On Sat, 06 Dec 2008 21:43:11 -0500, Damon Timm wrote:
>
> > On Sat, Dec 6, 2008 at 6:25 PM, Python Nutter
> <pythonnutter at gmail.com>
> > wrote:
> >> I'm on my phone so excuse the simple reply.
> From what I skimmed you are
> >> wrapping shell commands which is what I do all the
> time. Some hints. 1)
> >> look into popen or subprocess in place of execute
> for more flexibility.
> >> I use popen a lot and assigning a popen call to an
> object name let's
> >> you parse the output and make informed decisions
> depending on what the
> >> shell program outputs.
> >
> > So I took a peak at subprocess.Popen --> looks like
> that's the direction
> > I would be headed for parallel processes ... a real
> simple way to see it
> > work for me was:
> >
> > p2 =
> subprocess.Popen(["lame","--silent","test.wav","test.mp3"])
>
> > p3 =
> subprocess.Popen(["lame","--silent","test2.wav","test2.mp3"])
>
> > p2.wait()
> > p3.wait()
>
> I think when you do that (p2.wait() then p3.wait() ), if p3
> finishes
> first, you wouldn't start another p3 until p2 have
> finished (i.e. until
> p2.wait() returns) and if p2 finishes first, you
> wouldn't start another
> p2 until p3 finishes (i.e. until p3.wait() returns ).
>
> The solution would be to start and wait() the subprocessess
> in two
> threads. Use threading module or -- if you use python2.6 --
> the new
> multiprocessing module.
>
> Alternatively, you could do a "non-blocking
> wait", i.e. poll the thread.
>
> while True:
> if p1.poll(): # start another p1
> if p2.poll(): # start another p2
>
> >
> > top showed that both cores get busy and it takes half
> the time! So
> > that's great -- when I tried to add the flac
> decoding through stdout I
> > was able to accomplish it as well ... I was mimicing
> the command of
> > "flac --decode --stdout test.flac | lame -
> test.mp3" ... see:
> >
> > p =
> subprocess.Popen(["flac","--decode","--stdout","test.flac"],
> > stdout=subprocess.PIPE)
> > p2 =
> subprocess.Popen(["lame","-","test.mp3"],
> stdin=subprocess.PIPE)
> > p2.communicate(p.communicate()[0])
> >
> > That did the trick - it worked! However, it was
> *very* slow! The
> > python script has a "real" time of 2m22.504s
> whereas if I run it from
> > the command line it is only 0m18.594s. Not sure why
> this is ...
> >
> > The last piece of my puzzle though, I am having
> trouble wrapping my head
> > around ... I will have a list of files
> >
> ["file1.flac","file2.flac","file3.flac","etc"]
> and I want the program to
> > tackle compressing two at a time ... but not more than
> two at a time (or
> > four, or eight, or whatever) because that's not
> going to help me at all
> > (I have dual cores right now) ... I am having trouble
> thinking how I can
> > create the algorithm that would do this for me ...
> >
> > Thanks everyone. Maybe after a good night's sleep
> it will come to me.
> > If you have any ideas - would love to hear them.
>
>
>
> ------------------------------
>
> Message: 2
> Date: Sun, 7 Dec 2008 10:31:11 +0000 (UTC)
> From: Lie Ryan <lie.1296 at gmail.com>
> Subject: Re: [Tutor] Sorting on different fields
> To: tutor at python.org
> Message-ID: <ghg8lf$8b5$2 at ger.gmane.org>
> Content-Type: text/plain; charset=UTF-8
>
> On Sat, 06 Dec 2008 21:47:16 -0800, the New me wrote:
>
> > is there a straightforward example?
>
> >>> import operator
> >>> k = [[1, 2, 3, 4], [4, 3, 2, 1], [1, 3, 2, 4],
> [2, 4, 3, 1]]
> >>> sorted(l, key=operator.itemgetter(3, 2))
> [[4, 3, 2, 1], [2, 4, 3, 1], [1, 3, 2, 4], [1, 2, 3, 4]]
> >>> for k in sorted(l, key=operator.itemgetter(0,
> 2)): print k
> ...
> [1, 3, 2, 4]
> [1, 2, 3, 4]
> [2, 4, 3, 1]
> [4, 3, 2, 1]
> >>> def genkey(item):
> ... return item[2]
> ...
> >>> for k in sorted(l, key=genkey): print k
> ...
> [4, 3, 2, 1]
> [1, 3, 2, 4]
> [1, 2, 3, 4]
> [2, 4, 3, 1]
> >>> def genkey(item):
> ... return item[2], item[1]
> ...
> >>> for k in sorted(l, key=genkey): print k
> ...
> [4, 3, 2, 1]
> [1, 3, 2, 4]
> [1, 2, 3, 4]
> [2, 4, 3, 1]
>
>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 58, Issue 22
> *************************************
More information about the Tutor
mailing list