Simple threading

J

jrpfinch

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_secondspertick


g_secondspertick=5
g_datum=time.time()
g_currenttick=1
g_rawfile=open('inputashes.txt','r')
g_rawtext=g_rawfile.read()
g_overs=g_rawtext.split('<P>')
g_currentover=0


class ImapThread(threading.Thread):
def run(self):
global g_currenttick
if time.time() > (g_datum + (g_secondspertick *
g_currenttick)):
print "Ticked %s" % g_currenttick
g_currenttick=g_currenttick+1
print g_currenttick
sys.stdout.flush()
time.sleep(0.01)

def main():
ImapThread().start()
while 1:
pass

if __name__ == "__main__":
main()
 
H

hg

jrpfinch said:
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_secondspertick


g_secondspertick=5
g_datum=time.time()
g_currenttick=1
g_rawfile=open('inputashes.txt','r')
g_rawtext=g_rawfile.read()
g_overs=g_rawtext.split('<P>')
g_currentover=0


class ImapThread(threading.Thread):
def run(self):
global g_currenttick
if time.time() > (g_datum + (g_secondspertick *
g_currenttick)):
print "Ticked %s" % g_currenttick
g_currenttick=g_currenttick+1
print g_currenttick
sys.stdout.flush()
time.sleep(0.01)

def main():
ImapThread().start()
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_secondspertick


g_secondspertick=5
g_datum=time.time()
g_currenttick=1
#g_rawfile=open('inputashes.txt','r')
#g_rawtext=g_rawfile.read()
#g_overs=g_rawtext.split('<P>')
g_currentover=0


class ImapThread(threading.Thread):
def run(self):
while True:
global g_currenttick
if time.time() > (g_datum + (g_secondspertick *
g_currenttick)):
print "Ticked %s" % g_currenttick
g_currenttick=g_currenttick+1
print g_currenttick
sys.stdout.flush()
else:
print 'HERE'
time.sleep(0.01)

def main():
ImapThread().start()
while 1:
pass

if __name__ == "__main__":
main()
 
D

Dennis Lee Bieber

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 also don't understand /how/ to specify a global...
global g_datum
global g_rawfile
global g_rawtext
global g_overs
global g_currentover
global g_secondspertick
The "global" statement only applies INSIDE a code block, it does
nothing at the module (file) level. You don't need ANY of the above
class ImapThread(threading.Thread):
def run(self):
global g_currenttick

This "global" is correct; it indicates that "g_currenttick" is a
name bound to an object at the module level AND that the this block will
be changing the module level binding. "global" is not needed for
read-only/non-local names -- the interpreter search order will find
them.

The problem with the thread itself has been answered.
ImapThread().start()
while 1:
pass
Given that you essentially want to wait forever (there is no exit
from the thread [as corrected]), you could replace the above with

theThread = ImapThread()
theThread.start()
theThread.join() #wait until thread exits (which never happens)

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
G

Gabriel Genellina

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(threading.Thread):
def run(self):
global g_currenttick
if time.time() > (g_datum + (g_secondspertick *
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
 
G

Grant Edwards

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.
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.
 
A

Aahz

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 ([email protected]) <*> 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
 
F

Fredrik Lundh

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top