473,770 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Persistent Threads & Synchronisation

I appear to be having some problems with the isAlive() method of
detecting if a thread is alive/active/running or not. I'd be grateful
for any advice.

I have a visualisation program (which uses PyGame Extended [1]) that
presents content to the user and is meant to download the next batch of
content whilst the current one is being displayed. To achieve this, I
created a thread to perform the downloading in the background (a class
based on threading.Threa d).

They way I want it to work is this: The downloading thread, when
spawned, stays alive for the duration of the program. Occasionally the
main program will call a function in it to download the data and save it
as files on disk. Then, these files are loaded by the main thread.
When this has happened, the main thread calls another function in the
download thread to delete the temporary files. The same thing happens
next time a download is needed, when the user is looking at some other
content.

My problem is this: the downloading thread only seems to execute code
in a separate thread as long as the run() function (called by the
start() function) is running. This is as per the documentation, of
course. I am performing the download in the run() function, but the
file cleanup is still done with a separate call. This actually does
work, after the download is over, and run() has terminated, but I
believe it isn't happening in a separate thread anymore (as previously I
made a mistake and called run() directly, instead of start() and it
blocked the main program).

I have been using join() to wait until the download is complete at the
point in the main program where it is absolutely necessary for the
download to have finished. I'm trying to use the following code to
restart the downloading thread when I next need to use it:

if not self.preview_th read.isAlive():
self.preview_th read.start()

Unfortunately, isAlive() sometimes returns False when the thread is
actually still running. This means that my code crashes out, on the
assertion in threading.Threa d that self.__started is False. The
documentation [2] explains that the issue of threads being ``alive''
has ``intentionally been left vague'', which doesn't inspire confidence
:-S.

To work around this problem, I was thinking of doing this:

class PreviewThread(t hreading.Thread ):
. . .
def run(self):
self.download_d ata()
while not self.temp_files _cleaned:
pass
return
. . .
def cleanup(self):
. . .
self.temp_files _cleaned = True

This should allow the thread to remain alive until after the download
*and* cleanup have been completed, so that I can be sure it's safe to
restart it when I next need to download more data.

My ultimate question: is this the right way to do things? I don't like
the idea of making a loop such as that in the proposed run() function
above, it seems inefficient. I am also concerned that it won't let me
call other functions in the thread, perhaps.

I did wonder about using a Condition, but that seems to be more suited
for synchronising between threads, which isn't really the issue here
(and thus it seems like overkill for solving the problem of replacing
that loop with something more efficient, though could be a possibility,
I suppose).

It seems I'm missing something big and possibly obvious about how to
make threads persistent. I would like to know ``the right way'' to do
it and would really appreciate some advice on the subject.

Many thanks in advance!
[1] http://codereactor.net/projects/pygext/
[2] http://docs.python.org/lib/thread-objects.html
--
Matthew Tylee Atkinson <ma*****@agrip. org.uk>
Nov 26 '06 #1
2 2222
On Sun, 26 Nov 2006 19:38:20 +0000, Dennis Lee Bieber wrote:
Methods defined in a thread class but called from outside the
running thread run in the environment of the caller, not as part of the
thread (and if the method manipulates state you may run into conflicts).
Thanks very much; I'd arrived at this conclusion, but didn't know how to fix the
situation.
You CAN NOT restart threads... You have to recreate the whole thread
object. .start() can only be called ONCE on any thread object.
That clears a lot up.
clean(), next(), and quit() will be called by the main thread as
needed. The "preview thread" does nothing except wait for a command on
the queue, then performs the operation requested. The main thread will
have to ensure it calls the next() and clean() methods in proper
sequence.
Excellent; thanks for the example.
(How the main thread is informed of completion is something I
don't know from your post).
Currently, I am using locks (as in threading.Lock( )).
If you /need/ to have overlapped downloads and cleanups, you will
need TWO threads, and much more complex interlocking.
I don't, but thanks for the advice.

best regards,
--
Matthew Tylee Atkinson <ma*****@agrip. org.uk>
Nov 27 '06 #2
"Matthew Tylee Atkinson" <ma*****@agrip. org.ukwrote in message
news:ek******** **@news.freedom 2surf.net...
>I appear to be having some problems with the isAlive() method of
detecting if a thread is alive/active/running or not. I'd be grateful
for any advice.
Your comments about restartable threads got me thinking about generators.
While this does not strictly do threading, this little example uses a list
of generators, which save their state and pick up where they left off.
(Also, look into simpy, which has a similar concept, but much more
infrastructure support).

-- Paul
class Processor(objec t):
def __init__(self, id_):
self.id = id_
self.finished = False

def __str__(self):
return "Processor: %s" % self.id

def run(self):
def runImpl_(self):
runGen = self.runImpl()
while not self.finished:
try:
yield self.id,runGen. next()
except StopIteration:
self.finished = True
return runImpl_(self)

def runImpl(self):
times = 0
while times < self.id:
times += 1
yield times
import random

class RandomProcessor (Processor):
# just implement runImpl in subclass
def runImpl(self):
times = 0
while times < self.id:
times += 1
yield random.random()

def main():
# create list of processors
procList =[ (random.choice([True,False]) and
Processor(i) or RandomProcessor (i))
for i in range(10)]
procs = [ (p,p.run()) for p in procList ]

# context switch loop
while procs:

# cycle through all processors
for p in procs:
try:
ret = p[1].next()
print ret
except StopIteration:
pass

# remove any processors that have finished
procs = [ p for p in procs if not p[0].finished ]

main()

Nov 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1468
by: grahamo | last post by:
Hi, I realise that c++ knows nothing about threads however my question is related to an (excellent) article I was reading about threads and C++. For all intents and purposes we can forget the sample at hand is concerned about threads, it could be related to widgets for the purpose of this question (that's so I don't get flamed and told to go to comp.programming.threads :)
0
1655
by: Gomaw Beoyr | last post by:
Hello The book "C# Black Book" chapter about Threads says that a "background thread" cannot communicate directly with a visual element, e.g. a label, and thus has to use the BeginInvoke method, otherwise an exception will be thrown. The book also says: "Because a thread has its own process space, the addres of an object in one thread is not useful in another thread."
2
1771
by: Alex | last post by:
Dear colleagues I am programming an application which starts a lot of threads for some calculations. The problem that I have is that when I start up to 100 threads then everything works fine. Depending of number of jobs it might be that sometimes application need to start up to 4000 threads for this small jobs. You see probably already the problem. This is not good idea to start 5000 threads at the same time (memory). My idea is to...
2
916
by: Fekkai | last post by:
I have a question about threads: In the TCPClient help page I find the following explanation "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe." What does it mean, I need an example of something that is not safe.
2
289
by: Matthew Tylee Atkinson | last post by:
I appear to be having some problems with the isAlive() method of detecting if a thread is alive/active/running or not. I'd be grateful for any advice. I have a visualisation program (which uses PyGame Extended ) that presents content to the user and is meant to download the next batch of content whilst the current one is being displayed. To achieve this, I created a thread to perform the downloading in the background (a class based on...
41
2114
by: Carl J. Van Arsdall | last post by:
Hey everyone, I have a question about python threads. Before anyone goes further, this is not a debate about threads vs. processes, just a question. With that, are python threads reliable? Or rather, are they safe? I've had some strange errors in the past, I use threading.lock for my critical sections, but I wonder if that is really good enough. Does anyone have any conclusive evidence that python threads/locks are safe or unsafe?
7
1648
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I have a win app. this a form with a button. after pressing the button a new form is shown. when looading the form is being filled by data taken from a database. the problem is that it takes a few seconds, so a user has to wait a long time. i want to do something like that - when you have pressed the button on the first form a new window should appear ("please wait when loading"). in background the the main new form should be loading data...
4
1205
by: =?Utf-8?B?bG91aXM=?= | last post by:
Can someone please discuss "Synclock" or other methods to use for making a class 'thread safe". I need to have about 4 processes running (probably on backgroundworker threads) and all will need to call the same class for reading data (not SQL Server). If each worker thread creates an "instance" of the class, won't that be good enough? TIA
167
8349
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an object should run in its own thread, it should implement this mix-in class. Does this sound like plausible design decision? I'm surprised that C++ doesn't have such functionality, say in its STL. This absence of a thread/object relationship in...
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.