Problem in threading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gurpreet Sachdeva

    #1

    Problem in threading

    I have written a code to figure out the difference in excecution time
    of a func before and after using threading...

    Code:
    #!/usr/bin/env python
    
    import threading
    import time
    loops = [5000,5000]
    
    def loop(self, nsec):
    for i in range(1,nsec):
    t=i*5000
    s=t/10*15555
    
    def main():
    threads = []
    nloops = [0,1]
    for i in nloops:
    print '\nSpawning Thread',i,' value: ',loops[i]
    t = threading.Thread(target=loop,args=(i, loops[i]))
    threads.append(t)
    
    for i in nloops: # start threads
    threads[i].start()
    
    for i in nloops: # wait for all
    threads[i].join
    
    if __name__ == '__main__':
    start = time.clock()
    loop(1,5000)
    print 'Time Taken: ', time.clock()-start
    start = time.clock()
    main()
    print 'Time Taken: ', time.clock()-start
    Some times this code executes and some times it hangs to death :o(
    Am I am doing something wrong?? Also the difference of time is not much...
    How do we best optimize our task by using threads... please help...

    Thanks and Regards,
    Gurpreet Singh
  • Alex Martelli

    #2
    Re: Problem in threading

    Gurpreet Sachdeva <gurpreet.sachd eva@gmail.com> wrote:
    [color=blue]
    > for i in nloops: # wait for all
    > threads[i].join[/color]

    Missing () after 'join'.


    Alex

    Comment

    • Duncan Booth

      #3
      Re: Problem in threading

      Gurpreet Sachdeva wrote:
      [color=blue]
      > Also the difference of time is not much...
      > How do we best optimize our task by using threads... please help...
      >[/color]

      For most tasks splitting the processing into separate threads will result
      in an increase in the total time to complete the task. The only times when
      it may result in a decrease in the running time are when the time the task
      takes does not entirely depend on the time taken by the CPU, or when
      multiple CPU's are involved.

      Unfortunately the latter case isn't handled well by Python, so don't expect
      multiple CPU's to help speed up a multi-threaded Python program by very
      much. That leaves the former case: if your task has to stop and wait for
      something else to happen (e.g. data to be read from a network, or to be
      read from a disc file), then splitting it into multiple threads may allow
      the waits to be overlapped with useful processing which could result in an
      overall decrease in processing time.

      A good use of threads is to improve responsiveness of a system. For example
      if you ensure that GUI processing happens on a separate thread from some
      CPU intensive computation then you can ensure that the GUI remains
      responsive while the computation is running. It won't make the computation
      complete any faster (in fact it will probably be slower), but the user will
      remain happier. Similarly network applications are usually multi-threaded
      so that all requests get a fair chance to complete instead of having to
      wait for the slowest to complete before others can run.

      If you do have multiple processors available and want to speed up a Python
      program then you probably have to look at multiple processes rather than
      multiple threads. Alternatively you could move parts of the processing out
      of the Python environment by rewriting inner loops in C and releasing the
      interpreter lock, but that isn't usually the best option.

      Comment

      • Mike Meyer

        #4
        Re: Problem in threading

        Duncan Booth <duncan.booth@i nvalid.invalid> writes:
        [color=blue]
        > That leaves the former case: if your task has to stop and wait for
        > something else to happen (e.g. data to be read from a network, or to
        > be read from a disc file), then splitting it into multiple threads
        > may allow the waits to be overlapped with useful processing which
        > could result in an overall decrease in processing time.[/color]

        If you're on a Unix-like system, you'll use less aspirin if you do
        this kind of thing with async I/O and the select module. If you're on
        Windows, the restrictions on what you can select on make it much less
        useful. Python's threading models is pretty primitive. You get the C
        model (which is error-prone), the Java model (in 2.4, and also
        error-prone), or Queues. Queues aren't error-prone, but result in the
        same kind of behavior as you get with select: start I/O, do computing
        while I/O is going on, block until I/O is complete.

        <mike
        --
        Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
        Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

        Comment

        • Peter Hansen

          #5
          Re: Problem in threading

          Mike Meyer wrote:[color=blue]
          > Python's threading models is pretty primitive. You get the C
          > model (which is error-prone), the Java model (in 2.4, and also
          > error-prone), or Queues.[/color]

          Can you please expand on your words above? I have no idea
          what you are talking about with the "Java model" and your
          implication that it is something new in Python 2.4. As
          far as I know, nothing significant with respect to threads
          has changed in Python recently.

          -Peter

          Comment

          • Gurpreet Sachdeva

            #6
            Re: Problem in threading

            I wrote:[color=blue][color=green][color=darkred]
            >>> Also the difference of time is not much...
            >>> How do we best optimize our task by using threads... please help...[/color][/color][/color]

            Duncan Booth Wrote:[color=blue]
            >The only times when it may result in a decrease
            >in the running time... are when the time the task...when
            > multiple CPU's are involved.[/color]
            I fotgot to mention that I am running that script on a 4 node Open SSI
            cluster with 8 processors to do the job...
            [color=blue]
            > Unfortunately the latter case isn't handled well by Python[/color]
            Very Strange!
            [color=blue]
            > A good use of threads is to improve responsiveness of a system. For example
            > if you ensure that GUI processing happens on a separate thread from some
            > CPU intensive computation then you can ensure that the GUI remains
            > responsive while the computation is running. It won't make the computation
            > complete any faster (in fact it will probably be slower), but the user will
            > remain happier. Similarly network applications are usually multi-threaded
            > so that all requests get a fair chance to complete instead of having to
            > wait for the slowest to complete before others can run.[/color]

            So That means blindly using threads on any process won't help!
            [color=blue]
            >probably have to look at multiple processes rather than
            > multiple threads.[/color]

            How do I do that??? Does that mean forking the processes or does that
            mean to change the entire architecture to do small tasks in different
            processes???
            [color=blue]
            >Alternativel y you could move parts of the processing out
            > of the Python environment by rewriting inner loops in C[/color]

            Any reference/documentation for that???

            Thanks and Regards
            Gurpreet

            Comment

            • It's me

              #7
              Re: Problem in threading


              "Gurpreet Sachdeva" <gurpreet.sachd eva@gmail.com> wrote in message
              news:mailman.85 81.1104384222.5 135.python-list@python.org ...[color=blue]
              >
              > So That means blindly using threads on any process won't help!
              >[/color]

              It depends on what "help" means to you. Both Windows and Unix (and it's
              variances) are considered "thread-weak" OSes. So, using thread will come
              with some cost. The long gone IBM OS/2 is a classic example of a
              "thread-strong" OS.

              Still, a good use of thread would be, for example, a name-pipe server. See
              for instance:



              Here you will see a performance improvement when threads are being used.

              The example you listed isn't a good use of thread for performance
              improvement sake.



              Comment

              • Gurpreet Sachdeva

                #8
                Re: Problem in threading

                >>> So That means blindly using threads on any process won't help![color=blue]
                > It depends on what "help" means to you.[/color]

                Help means to improve processing speed in achieving a particular
                task... *Help* here also means that I have a processor farm, how do I
                best use them to get maximum processing speed out of them...
                [color=blue]
                > The example you listed isn't a good use of thread for performance[/color]

                The example was a dummy one to become friendly with Threads and to
                understand the working/power of them... The example which will be
                finally used with threads take 5 Days to complete... I want to convert
                that in few hours!

                Long Journey ahead...

                Thanks,
                Garry

                Comment

                • Steve Holden

                  #9
                  Re: Problem in threading

                  Gurpreet Sachdeva wrote:
                  [color=blue][color=green][color=darkred]
                  >>>>So That means blindly using threads on any process won't help![/color]
                  >>
                  >>It depends on what "help" means to you.[/color]
                  >
                  >
                  > Help means to improve processing speed in achieving a particular
                  > task... *Help* here also means that I have a processor farm, how do I
                  > best use them to get maximum processing speed out of them...
                  >[/color]
                  And that's the crux of your problem.

                  Firstly, you've discovered that attempts to partition a task using
                  threads won't work well with a compute-intensive algorithm, since
                  multiple threads will simply contend against each other for CPU in a
                  single process.

                  This is not assisted by Python's use of a global interpreter lock (GIL)
                  to ensure thread-safety.

                  A thread can release the GIL, but typically it will do this only when
                  it's involved in some blocking operation. This means that threading can
                  be useful to speed up network operations, for example, but even then you
                  might get a better speedup using explicitly asynchronous techniques
                  based on non-blocking sockets.[color=blue]
                  >[color=green]
                  >>The example you listed isn't a good use of thread for performance[/color]
                  >
                  >
                  > The example was a dummy one to become friendly with Threads and to
                  > understand the working/power of them... The example which will be
                  > finally used with threads take 5 Days to complete... I want to convert
                  > that in few hours!
                  >[/color]
                  In that case you definitely need to be looking at multiprocess
                  algorithms if you are sticking with Python. Since each process has its
                  own copy of the interpreter, they each also have their own GIL, and so
                  the operating system will be able to schedule the processes in parallel
                  on separate CPUs.
                  [color=blue]
                  > Long Journey ahead...
                  >[/color]
                  Indeed, but an interesting one, no doubt. Good luck.

                  regards
                  Steve
                  --
                  Steve Holden http://www.holdenweb.com/
                  Python Web Programming http://pydish.holdenweb.com/
                  Holden Web LLC +1 703 861 4237 +1 800 494 3119

                  Comment

                  • David Bolen

                    #10
                    Re: Problem in threading

                    "It's me" <itsme@yahoo.co m> writes:
                    [color=blue]
                    > It depends on what "help" means to you. Both Windows and Unix (and it's
                    > variances) are considered "thread-weak" OSes. So, using thread will come
                    > with some cost. The long gone IBM OS/2 is a classic example of a
                    > "thread-strong" OS.[/color]
                    (...)

                    Interesting - can you clarify what you perceive as the differences
                    between a thread-weak and thread-strong OS? If given the choice, I
                    would probably refer to Windows (at least NT based systems, let's
                    ignore 9x) as thread-strong, and yes, often think of Windows as
                    preferring thread based solutions, while Unix would often prefer
                    process based.

                    Windows is far more efficient at handling large numbers of threads
                    than it is processes, with much less overhead and there is lots of
                    flexibility in terms of managing threads and their resources. Threads
                    are first class OS objects at the kernel and scheduler level (waitable
                    and manageable).

                    I can't think of anything offhand specific that OS/2 did with respect
                    to threads that isn't as well supported by current Win32 systems.

                    -- David

                    Comment

                    • It's me

                      #11
                      Re: Problem in threading

                      That's an OT topic. :=)

                      There were lots of discussions about this topic in the old days. No need
                      to dive into it again.

                      Windows context switching overhead is very high. You would be lucky to get
                      it down to the mid-30ms. OS/2 can get it down to less then 10. And for
                      OS/2, thread swithing time is only a few machine instructions... .OT.OT.


                      "David Bolen" <db3l@fitlinxx. com> wrote in message
                      news:uacrvhfdh. fsf@fitlinxx.co m...[color=blue]
                      > "It's me" <itsme@yahoo.co m> writes:
                      >[color=green]
                      > > It depends on what "help" means to you. Both Windows and Unix (and[/color][/color]
                      it's[color=blue][color=green]
                      > > variances) are considered "thread-weak" OSes. So, using thread will[/color][/color]
                      come[color=blue][color=green]
                      > > with some cost. The long gone IBM OS/2 is a classic example of a
                      > > "thread-strong" OS.[/color]
                      > (...)
                      >
                      > Interesting - can you clarify what you perceive as the differences
                      > between a thread-weak and thread-strong OS? If given the choice, I
                      > would probably refer to Windows (at least NT based systems, let's
                      > ignore 9x) as thread-strong, and yes, often think of Windows as
                      > preferring thread based solutions, while Unix would often prefer
                      > process based.
                      >
                      > Windows is far more efficient at handling large numbers of threads
                      > than it is processes, with much less overhead and there is lots of
                      > flexibility in terms of managing threads and their resources. Threads
                      > are first class OS objects at the kernel and scheduler level (waitable
                      > and manageable).
                      >
                      > I can't think of anything offhand specific that OS/2 did with respect
                      > to threads that isn't as well supported by current Win32 systems.
                      >
                      > -- David[/color]


                      Comment

                      • Mike Meyer

                        #12
                        Re: Problem in threading

                        Peter Hansen <peter@engcorp. com> writes:
                        [color=blue]
                        > Mike Meyer wrote:[color=green]
                        >> Python's threading models is pretty primitive. You get the C
                        >> model (which is error-prone), the Java model (in 2.4, and also
                        >> error-prone), or Queues.[/color]
                        > Can you please expand on your words above? I have no idea
                        > what you are talking about with the "Java model" and your
                        > implication that it is something new in Python 2.4. As
                        > far as I know, nothing significant with respect to threads
                        > has changed in Python recently.[/color]

                        See <URL: http://docs.python.org/lib/module-threading.html > which
                        clearly has the words "New in 2.4" on it. It also says that the
                        threading module is loosely based on the Java model. I may have
                        misinterpreted what the "New in 2.4" applies to, which means that the
                        comment about the Java model being new in 2.4 would be wrong.

                        As for error-prone, both models require the programmer to always
                        obtain locks around critical sections, and to obtain them in an order
                        that prevents deadlocks, and release them in an order that prevents
                        starvation.

                        There are threading models that don't require the programmer to get
                        the locking right. In those, you basically declare an object as
                        running on a different thread, and the compiler provides the
                        appropriate locks for each method. I went looking for a reference to
                        one such method, but couldn't turn it up, partly because I don't
                        recall whether I saw it on usenet or a mail list.

                        <mike
                        --
                        Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                        Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                        Comment

                        • Peter Hansen

                          #13
                          Re: Problem in threading

                          Mike Meyer wrote:[color=blue]
                          > See <URL: http://docs.python.org/lib/module-threading.html > which
                          > clearly has the words "New in 2.4" on it. It also says that the
                          > threading module is loosely based on the Java model. I may have
                          > misinterpreted what the "New in 2.4" applies to, which means that the
                          > comment about the Java model being new in 2.4 would be wrong.[/color]

                          You have misinterpreted that. The "New in 2.4" part is not
                          so much "on" that page as it is "in" it, and specifically
                          it is right in the section on the new-to-2.4 class "local".

                          The rest of the threading module is basically unchanged since
                          a number of versions ago, but thanks for your clarification. :-)

                          -Peter

                          Comment

                          Working...