Hi,
I just have a simple question about threads. My classes inherits from
threading.Thread class. I am calling threading.Thread.run() method to
spawn a few threads to parallel some parts of my program. No thread
re-use, pooling, joining ... just plainly spawn a thread, run a routine.
So, at the end of run(), what happens to the thread? Just die?
While I am on it, can threading.Thread.run() accept any parameters?
My current implementation may be ugly. I have a class
class myThread(threading.Thread):
def __init__(self, func):
self.func = func
threading.Thread.__init__(self)
def run(self):
print '%s function running' % self.func
self.func()
which is used in
class myClass: #using myThread
def __init__(self): pass
def aFunc(self): pass
def bFunc(self): pass
def runAll(self):
myThread(self.aFunc).start()
myThread(self.bFunc).start()
if __name__=='__main__': myClass().runAll()
Is this a good way?
Thanks and cheers
Maurice 8 3156
Maurice LING enlightened us with: So, at the end of run(), what happens to the thread? Just die?
Yep.
While I am on it, can threading.Thread.run() accept any parameters?
Nope. Pass them to the constructor and remember them.
class myThread(threading.Thread): def __init__(self, func): self.func = func threading.Thread.__init__(self) def run(self): print '%s function running' % self.func self.func()
You don't need to do this, since you can pass a callable object to the
Thread constructor. Read the first lines of http://docs.python.org/lib/thread-objects.html again. That would
change your code to:
class myClass:
def __init__(self): pass
def aFunc(self): pass
def bFunc(self): pass
def runAll(self):
threading.Thread(self.aFunc).start()
threading.Thread(self.bFunc).start()
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Maurice LING wrote: Hi,
I just have a simple question about threads. My classes inherits from threading.Thread class. I am calling threading.Thread.run() method to spawn a few threads to parallel some parts of my program. No thread re-use, pooling, joining ... just plainly spawn a thread, run a routine.
So, at the end of run(), what happens to the thread? Just die?
Just die, mostly. It may do a bit of clean-up, setting its
affairs in order, arraning for burial/cremation/organ-donation
and such, to avoid leaving any problem for posterity.
Incidentally, the "threading" module, with its "Thread" class,
has one major advantage over the "thread" module: the "setDaemon"
method of class Thread.
While I am on it, can threading.Thread.run() accept any parameters?
Not unless you override it; but you can pass parameters when
constructing the Thread, like this:
t = Thread(target=work_to_do, args=(67, 'Bill Johnston'), kwargs=some_dict)
t.start() will then execute the default Thread.run(), which
will execute work_to_do with two positional arguments -- 67 and
'Bill Johston' -- and whatever key-word arguments were in some_dict.
If you override 'Thread.run', you can define your override to take
whatever parameters you want.
My current implementation may be ugly. I have a class
class myThread(threading.Thread): def __init__(self, func): self.func = func threading.Thread.__init__(self) def run(self): print '%s function running' % self.func self.func()
[...]
Is this a good way?
I don't see anything wrong with it, though to me, it seems a
little heavy. To run your func, all you need to do is:
Thread(target=func).start()
Though nine times out of ten, you'd want:
t = Thread(target=func, args=(arg1, arg2, and_so_on))
t.setDaemon(True)
t.start()
If you really need that "print", you could build it into your
'target' function; or you could subclass Thread more generally,
so your subclass adds the print and behaves exactly the same
otherwise. I regard debugging as important, but I do not
believe that this particular print warrants the general
inclusion implied.
Other Pythoners have disagree with me on various matters at
issue here. In particular, others have disagreed with my
advocacy of multiple lines of execution.
--
--Bryan
Maurice LING wrote: Hi,
I just have a simple question about threads. My classes inherits from threading.Thread class. I am calling threading.Thread.run() method to spawn a few threads to parallel some parts of my program. No thread re-use, pooling, joining ... just plainly spawn a thread, run a routine.
So, at the end of run(), what happens to the thread? Just die?
As far as you are concerned, yes. They really just return to
wherever they came from. How they get created before they call
the run() method, and where they go after run() returns is down
to the implementation of the interpreter. Your code never sees
them before or after. While I am on it, can threading.Thread.run() accept any parameters?
No. But you can override it in a subclass.
My current implementation may be ugly. I have a class
class myThread(threading.Thread): def __init__(self, func): self.func = func threading.Thread.__init__(self) def run(self): print '%s function running' % self.func self.func()
which is used in
class myClass: #using myThread def __init__(self): pass def aFunc(self): pass def bFunc(self): pass def runAll(self): myThread(self.aFunc).start() myThread(self.bFunc).start()
There is a school of thought that says that a derived object
should never be altered such that it cannot be used as its parent
could. I happen to agree largely with this. Now, your MyThread
implementation is _reducing_ the functionality of its ancestor
Thread object such that you could not use a myThread in place of
a Thread. I believe that you should not be subclassing Thread to
do this. Your myClass doesn't need it anyway. Look at this
modified myClass:
class myClass2:
def aFunc(self): pass
def bFunc(self): pass
def runAll(self):
threading.Thread(target=self.aFunc).start()
threading.Thread(target=self.bFunc).start()
Steve My current implementation may be ugly. I have a class
class myThread(threading.Thread): def __init__(self, func): self.func = func threading.Thread.__init__(self) def run(self): print '%s function running' % self.func self.func()
which is used in
class myClass: #using myThread def __init__(self): pass def aFunc(self): pass def bFunc(self): pass def runAll(self): myThread(self.aFunc).start() myThread(self.bFunc).start() There is a school of thought that says that a derived object should never be altered such that it cannot be used as its parent could. I happen to agree largely with this. Now, your MyThread implementation is _reducing_ the functionality of its ancestor Thread object such that you could not use a myThread in place of a Thread. I believe that you should not be subclassing Thread to do this. Your myClass doesn't need it anyway. Look at this modified myClass:
class myClass2: def aFunc(self): pass def bFunc(self): pass def runAll(self): threading.Thread(target=self.aFunc).start() threading.Thread(target=self.bFunc).start()
Thanks everyone. Furthering that, is the following legal?
class myClass3:
def aFunc(self, a): pass
def bFunc(self, b): pass
def runAll(self, a, b):
threading.Thread(target=self.aFunc, args = (a)).start()
threading.Thread(target=self.bFunc, args = (b)).start()
I do have another dumb question which is OT here. Say aFunc method
instantiates a SOAP server that serves forever, will it prevent bFunc
from running as a separate thread?
For example,
class myClass4:
def repeat(self, s): return s+s
def aFunc(self, a):
import SOAPpy
serv = SOAPpy.SOAPServer((a[0], a[1]))
serv.registerFunction(repeat)
serv.serve_forever()
def bFunc(self, b): pass
def runAll(self, a, b):
threading.Thread(target=self.aFunc, args = (a)).start()
threading.Thread(target=self.bFunc, args = (b)).start()
if __name__=='__main__': myClass4().runAll(['localhost', 8000], 'hi')
Will the 2nd thread (bFunc) ever run since the 1st thread is running
forever? Intuitively, I think that both threads will run but I just want
to be doubly sure, because some of my program logic depends on the 2nd
thread running while the 1st thread acts as a SOAP server or something.
Thanks and Cheers
Maurice
"Maurice LING" <ma*********@acm.org> wrote in message
news:dg**********@domitilla.aioe.org... I do have another dumb question which is OT here. Say aFunc method instantiates a SOAP server that serves forever, will it prevent bFunc from running as a separate thread?
If the SOAP server thread never sleeps or block, it will effectively stop
everything else in your program by eating all the CPU time available. If it
does some IO and other OS functions, probably not because it is likely to
block on those - I do not know SOAPpy in detail, but it being a socket-based
server it should end up in a select loop somewhere. i.e. block when no work
is available. which is what you want.
For example,
class myClass4: def repeat(self, s): return s+s def aFunc(self, a): import SOAPpy serv = SOAPpy.SOAPServer((a[0], a[1])) serv.registerFunction(repeat) serv.serve_forever() def bFunc(self, b): pass def runAll(self, a, b): threading.Thread(target=self.aFunc, args = (a)).start() threading.Thread(target=self.bFunc, args = (b)).start()
if __name__=='__main__': myClass4().runAll(['localhost', 8000], 'hi')
Will the 2nd thread (bFunc) ever run since the 1st thread is running forever? Intuitively, I think that both threads will run but I just want to be doubly sure, because some of my program logic depends on the 2nd thread running while the 1st thread acts as a SOAP server or something.
Both should run independently, sharing the CPU-time available for your
application. Remember "main" is a thread too, so you will want "main" to
hang around while your threads are running and you will want "main" to block
on something also, thread.join(), time.sleep(), command line parser e.t.c.
whatever is natural.
Frithiof Andreas Jensen wrote: "Maurice LING" <ma*********@acm.org> wrote in message news:dg**********@domitilla.aioe.org...
I do have another dumb question which is OT here. Say aFunc method instantiates a SOAP server that serves forever, will it prevent bFunc from running as a separate thread?
If the SOAP server thread never sleeps or block, it will effectively stop everything else in your program by eating all the CPU time available. If it does some IO and other OS functions, probably not because it is likely to block on those - I do not know SOAPpy in detail, but it being a socket-based server it should end up in a select loop somewhere. i.e. block when no work is available. which is what you want.
For example,
class myClass4: def repeat(self, s): return s+s def aFunc(self, a): import SOAPpy serv = SOAPpy.SOAPServer((a[0], a[1])) serv.registerFunction(repeat) serv.serve_forever() def bFunc(self, b): pass def runAll(self, a, b): threading.Thread(target=self.aFunc, args = (a)).start() threading.Thread(target=self.bFunc, args = (b)).start()
if __name__=='__main__': myClass4().runAll(['localhost', 8000], 'hi')
Will the 2nd thread (bFunc) ever run since the 1st thread is running forever? Intuitively, I think that both threads will run but I just want to be doubly sure, because some of my program logic depends on the 2nd thread running while the 1st thread acts as a SOAP server or something.
Both should run independently, sharing the CPU-time available for your application. Remember "main" is a thread too, so you will want "main" to hang around while your threads are running and you will want "main" to block on something also, thread.join(), time.sleep(), command line parser e.t.c. whatever is natural.
Somehow I cannot reconcile your replies because I am essentially asking
the same thing and expanding on the original question with an example of
what I am trying to do, but the replies seems contradictory. Do you mind
to explain a bit more?
thanks
Maurice
Frithiof Andreas Jensen wrote: "Maurice LING" <ma*********@acm.org> wrote in message news:dg**********@domitilla.aioe.org...
I do have another dumb question which is OT here. Say aFunc method instantiates a SOAP server that serves forever, will it prevent bFunc from running as a separate thread?
If the SOAP server thread never sleeps or block, it will effectively stop everything else in your program by eating all the CPU time available. If it does some IO and other OS functions, probably not because it is likely to block on those - I do not know SOAPpy in detail, but it being a socket-based server it should end up in a select loop somewhere. i.e. block when no work is available. which is what you want.
For example,
class myClass4: def repeat(self, s): return s+s def aFunc(self, a): import SOAPpy serv = SOAPpy.SOAPServer((a[0], a[1])) serv.registerFunction(repeat) serv.serve_forever() def bFunc(self, b): pass def runAll(self, a, b): threading.Thread(target=self.aFunc, args = (a)).start() threading.Thread(target=self.bFunc, args = (b)).start()
if __name__=='__main__': myClass4().runAll(['localhost', 8000], 'hi')
Will the 2nd thread (bFunc) ever run since the 1st thread is running forever? Intuitively, I think that both threads will run but I just want to be doubly sure, because some of my program logic depends on the 2nd thread running while the 1st thread acts as a SOAP server or something.
Both should run independently, sharing the CPU-time available for your application. Remember "main" is a thread too, so you will want "main" to hang around while your threads are running and you will want "main" to block on something also, thread.join(), time.sleep(), command line parser e.t.c. whatever is natural.
Somehow I cannot reconcile your replies because I am essentially asking
the same thing and expanding on the original question with an example of
what I am trying to do, but the replies seems contradictory. Do you mind
to explain a bit more?
thanks
Maurice
Frithiof Andreas Jensen wrote: "Maurice LING" <ma*********@acm.org> wrote in message news:dg**********@domitilla.aioe.org...
I do have another dumb question which is OT here. Say aFunc method instantiates a SOAP server that serves forever, will it prevent bFunc from running as a separate thread?
If the SOAP server thread never sleeps or block, it will effectively stop everything else in your program by eating all the CPU time available. If it does some IO and other OS functions, probably not because it is likely to block on those - I do not know SOAPpy in detail, but it being a socket-based server it should end up in a select loop somewhere. i.e. block when no work is available. which is what you want.
For example,
class myClass4: def repeat(self, s): return s+s def aFunc(self, a): import SOAPpy serv = SOAPpy.SOAPServer((a[0], a[1])) serv.registerFunction(repeat) serv.serve_forever() def bFunc(self, b): pass def runAll(self, a, b): threading.Thread(target=self.aFunc, args = (a)).start() threading.Thread(target=self.bFunc, args = (b)).start()
if __name__=='__main__': myClass4().runAll(['localhost', 8000], 'hi')
Will the 2nd thread (bFunc) ever run since the 1st thread is running forever? Intuitively, I think that both threads will run but I just want to be doubly sure, because some of my program logic depends on the 2nd thread running while the 1st thread acts as a SOAP server or something.
Both should run independently, sharing the CPU-time available for your application. Remember "main" is a thread too, so you will want "main" to hang around while your threads are running and you will want "main" to block on something also, thread.join(), time.sleep(), command line parser e.t.c. whatever is natural.
Somehow I cannot reconcile your replies because I am essentially asking
the same thing and expanding on the original question with an example of
what I am trying to do, but the replies seems contradictory. Do you mind
to explain a bit more?
thanks
Maurice This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: adeger |
last post by:
Having trouble with my first forays into threads. Basically, the
threads don't seem to be working in parallel (or you might say are
blocking). I've boiled my problems to the following short code...
|
by: Gilles Leblanc |
last post by:
Hi I have started a small project with PyOpenGL. I am wondering what
are the options for a GUI. So far I checked PyUI but it has some
problems with 3d rendering outside the Windows platform.
I...
|
by: Ivan |
last post by:
Hi
I have following problem: I'm creating two threads who are performing some
tasks. When one thread finished I would like to restart her again (e.g. new
job). Following example demonstrates...
|
by: Matthew Groch |
last post by:
Hi all,
I've got a server that handles a relatively high number of concurrent
transactions (on the magnitude of 1000's per second). Client
applications establish socket connections with the...
|
by: Razzie |
last post by:
Hi all,
A question from someone on a website got me thinking about this, and I
wondered if anyone could explain this.
A System.Threading.Timer object is garbage collected if it has no...
| |
by: droopytoon |
last post by:
Hi,
I start a new thread (previous one was "thread timing") because I have
isolated my problem.
It has nothing to do with calling unmanaged C++ code (I removed it in a
test application).
I...
|
by: mareal |
last post by:
I have noticed how the thread I created just stops running. I have added
several exceptions to the thread
System.Threading.SynchronizationLockException
System.Threading.ThreadAbortException...
|
by: Bob Day |
last post by:
Using vs2003, vb.net
I start a thread, giving it a name before start. Code snippet:
'give each thread a unique name (for later identification)
Trunk_Thread.Name = "Trunk_0_Thread"
' allow...
|
by: Charles Law |
last post by:
My first thought was to call
WorkerThread.Suspend
but the help cautions against this (for good reason) because the caller has
no control over where the thread actually stops, and it might have...
|
by: John Nagle |
last post by:
There's no way to set thread priorities within Python, is there?
We have some threads that go compute-bound, and would like to
reduce their priority slightly so the other operations, like...
|
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,...
| |
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...
|
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...
|
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...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |