Simple threading

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

    #1

    Simple threading

    I'm just getting started on threading and was wondering why the
    following code does not work (i know globals is bad style - I'll
    eliminate them eventually). All I get is a blank cursor flashing.

    Many thanks

    Jon

    import threading
    import sys
    import time
    global g_datum
    global g_rawfile
    global g_rawtext
    global g_overs
    global g_currentover
    global g_secondspertic k


    g_secondspertic k=5
    g_datum=time.ti me()
    g_currenttick=1
    g_rawfile=open( 'inputashes.txt ','r')
    g_rawtext=g_raw file.read()
    g_overs=g_rawte xt.split('<P>')
    g_currentover=0


    class ImapThread(thre ading.Thread):
    def run(self):
    global g_currenttick
    if time.time() (g_datum + (g_secondsperti ck *
    g_currenttick)) :
    print "Ticked %s" % g_currenttick
    g_currenttick=g _currenttick+1
    print g_currenttick
    sys.stdout.flus h()
    time.sleep(0.01 )

    def main():
    ImapThread().st art()
    while 1:
    pass

    if __name__ == "__main__":
    main()

  • hg

    #2
    Re: Simple threading

    jrpfinch wrote:
    I'm just getting started on threading and was wondering why the
    following code does not work (i know globals is bad style - I'll
    eliminate them eventually). All I get is a blank cursor flashing.
    >
    Many thanks
    >
    Jon
    >
    import threading
    import sys
    import time
    global g_datum
    global g_rawfile
    global g_rawtext
    global g_overs
    global g_currentover
    global g_secondspertic k
    >
    >
    g_secondspertic k=5
    g_datum=time.ti me()
    g_currenttick=1
    g_rawfile=open( 'inputashes.txt ','r')
    g_rawtext=g_raw file.read()
    g_overs=g_rawte xt.split('<P>')
    g_currentover=0
    >
    >
    class ImapThread(thre ading.Thread):
    def run(self):
    global g_currenttick
    if time.time() (g_datum + (g_secondsperti ck *
    g_currenttick)) :
    print "Ticked %s" % g_currenttick
    g_currenttick=g _currenttick+1
    print g_currenttick
    sys.stdout.flus h()
    time.sleep(0.01 )
    >
    def main():
    ImapThread().st art()
    while 1:
    pass
    >
    if __name__ == "__main__":
    main()
    >
    Run gets called only once: you need to put your logic in a "while True"
    or something equivalent/define some escape clause.

    Right now you just get into the implicit "else" and get out.

    hg

    import time
    global g_datum
    global g_rawfile
    global g_rawtext
    global g_overs
    global g_currentover
    global g_secondspertic k


    g_secondspertic k=5
    g_datum=time.ti me()
    g_currenttick=1
    #g_rawfile=open ('inputashes.tx t','r')
    #g_rawtext=g_ra wfile.read()
    #g_overs=g_rawt ext.split('<P>' )
    g_currentover=0


    class ImapThread(thre ading.Thread):
    def run(self):
    while True:
    global g_currenttick
    if time.time() (g_datum + (g_secondsperti ck *
    g_currenttick)) :
    print "Ticked %s" % g_currenttick
    g_currenttick=g _currenttick+1
    print g_currenttick
    sys.stdout.flus h()
    else:
    print 'HERE'
    time.sleep(0.01 )

    def main():
    ImapThread().st art()
    while 1:
    pass

    if __name__ == "__main__":
    main()

    Comment

    • jrpfinch

      #3
      Re: Simple threading

      many thanks - works perfectly now

      Comment

      • jrpfinch

        #4
        Re: Simple threading

        Thank you for your help - the application is proceeding well.

        Comment

        • Gabriel Genellina

          #5
          Re: Simple threading

          At Thursday 23/11/2006 12:28, jrpfinch wrote:
          >I'm just getting started on threading and was wondering why the
          >following code does not work (i know globals is bad style - I'll
          >eliminate them eventually). All I get is a blank cursor flashing.
          You've got your example already working.
          Globals are bad style, but worse, threads and globals don't mix very
          well: you need some sort of syncronization for accessing globals
          (specially for writing them).
          >class ImapThread(thre ading.Thread):
          def run(self):
          global g_currenttick
          if time.time() (g_datum + (g_secondsperti ck *
          >g_currenttick) ):
          print "Ticked %s" % g_currenttick
          g_currenttick=g _currenttick+1
          print g_currenttick
          Having more than one thread, g_currenttick could have been modified
          *after* you read its value and *before* you write it back, so you
          lose the count.


          --
          Gabriel Genellina
          Softlab SRL

          _______________ _______________ _______________ _____
          Correo Yahoo!
          Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
          ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

          Comment

          • Grant Edwards

            #6
            Re: Simple threading

            On 2006-11-25, Gabriel Genellina <gagsl-py@yahoo.com.ar wrote:
            >>I'm just getting started on threading and was wondering why the
            >>following code does not work (i know globals is bad style - I'll
            >>eliminate them eventually). All I get is a blank cursor flashing.
            >
            You've got your example already working. Globals are bad
            style, but worse, threads and globals don't mix very well: you
            need some sort of syncronization for accessing globals
            (specially for writing them).
            That depends on the type of the global and how they're used.
            Re-binding a name is always an atomic operation. Modifying
            many mutable objects is atomic.
            >>class ImapThread(thre ading.Thread):
            > def run(self):
            > global g_currenttick
            > if time.time() (g_datum + (g_secondsperti ck *
            >>g_currenttick )):
            > print "Ticked %s" % g_currenttick
            > g_currenttick=g _currenttick+1
            > print g_currenttick
            >
            Having more than one thread, g_currenttick could have been modified
            *after* you read its value and *before* you write it back, so you
            lose the count.
            Right. Reading an integer object in one statement and writing
            it in a subsequent statement is not an atomic opteration unless
            protected by some synchronization mechanism.

            --
            Grant Edwards
            grante@visi.com

            Comment

            • Aahz

              #7
              Re: Simple threading

              In article <12mgkd6e5cp5l1 6@corp.supernew s.com>,
              Grant Edwards <grante@visi.co mwrote:
              >
              >Re-binding a name is always an atomic operation. Modifying
              >many mutable objects is atomic.
              You know this, but just to make clear: rebinding attributes of an object
              (which are also sometimes called names) is not necessarily an atomic
              operation. Moreover, only a plain rebinding operation is atomic;
              augmented assignment is frequently not atomic.
              --
              Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

              "In many ways, it's a dull language, borrowing solid old concepts from
              many other languages & styles: boring syntax, unsurprising semantics,
              few automatic coercions, etc etc. But that's one of the things I like
              about it." --Tim Peters on Python, 16 Sep 1993

              Comment

              • Fredrik Lundh

                #8
                Re: Simple threading

                Grant Edwards wrote:
                That depends on the type of the global and how they're used.
                Re-binding a name is always an atomic operation. Modifying
                many mutable objects is atomic.
                footnote: for more on this topic, see this FAQ entry:



                and this recent python-dev thread that discusses that entry:



                </F>

                Comment

                Working...