473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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_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
7 1112
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
many thanks - works perfectly now

Nov 23 '06 #3
Thank you for your help - the application is proceeding well.

Nov 24 '06 #4
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
3
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
3
by: Dara Durum | last post by:
Hi ! Py2.4, Win32. I need to optimize a program that have a speciality: hash (MD5/SHA1) the file contents (many files). Now I do this in a simple python program, because (what a pity) the...
1
by: Seun Osewa | last post by:
Hello, I've tried to run several threading examples in Python 2.5.1 (with Stackless) For example: import threading theVar = 1 class MyThread ( threading.Thread ):
3
by: writser | last post by:
hey all, For my study I'm writing a simple threaded webcrawler and I am trying to do this in python. But somehow, using threads causes IDLE to crash on Windows XP (with the latest python...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.