Simple threading
hg
hg at nospam.com
Thu Nov 23 10:30:53 EST 2006
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_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()
More information about the Python-list
mailing list