By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

Simple threading

P: n/a
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()

Nov 23 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
hg
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()

Nov 23 '06 #2

P: n/a
many thanks - works perfectly now

Nov 23 '06 #3

P: n/a
Thank you for your help - the application is proceeding well.

Nov 24 '06 #4

P: n/a
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(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
Nov 25 '06 #5

P: n/a
On 2006-11-25, Gabriel Genellina <ga******@yahoo.com.arwrote:
>>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(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.
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
gr****@visi.com

Nov 25 '06 #6

P: n/a
In article <12*************@corp.supernews.com>,
Grant Edwards <gr****@visi.comwrote:
>
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 (aa**@pythoncraft.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
Nov 25 '06 #7

P: n/a
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:

http://effbot.org/pyfaq/what-kinds-o...hread-safe.htm

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

http://mail.python.org/pipermail/pyt...ead.html#69981

</F>

Nov 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.