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

threading, how to?

P: n/a
Hi,

I am trying to understand how to use threading in Python. I get
threading as a concept, but not the implementation.

In order to start threading, do you call it as a separate function,
which will then be applied to the rest of the code (functions) or do
you open threading in each function. This all can probably be answered
by 'How python threads different functions'?

Hope if somebody can drop me a few lines. I've been trying with
different tutorials, but still do not understand.

Cheers,

Stevan

Apr 21 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"akrapus" <ak****@gmail.com> writes:
Hi,

I am trying to understand how to use threading in Python. I get
threading as a concept, but not the implementation.

In order to start threading, do you call it as a separate function,
which will then be applied to the rest of the code (functions) or do
you open threading in each function. This all can probably be answered
by 'How python threads different functions'?


Python doesn't automatically thread anything. Threading in Python is
manual. The idea is that you run your own function in a separate thread
of execution that you create. All the functions that you call from the
mentioned function (and those that they call, etc.) will then run in
those thread of execution.

You can create as many threads (probably up to some limit) as you wish
and call either the same function from them, or different functions,
depending on your application requirements.

-- Sergei.

Apr 21 '06 #2

P: n/a
Thanks for reply.

So would it be implemented as follows:

Func 1
Func 2
Func 3

Thread for Func 1
Thread for Func 2
Thread for Func 3

Cheers

Stevan

Apr 21 '06 #3

P: n/a
akrapus wrote:
Thanks for reply.

So would it be implemented as follows:

Func 1
Func 2
Func 3

Thread for Func 1
Thread for Func 2
Thread for Func 3


Could be, but the you woul most probably subclass threading.Thread and
override the run-method.

However, often it is done like this:

def some_threaded_function():
while though_shalt_work():
do_work()
for i in xrange(THRAD_NUM):
t = threading.Thread(target=some_threaded_function)
t.start()
Diez
Apr 21 '06 #4

P: n/a
"akrapus" <ak****@gmail.com> writes:
Thanks for reply.

So would it be implemented as follows:

Func 1
Func 2
Func 3

Thread for Func 1
Thread for Func 2
Thread for Func 3


Yes, if you wish to run every of your 3 functions in a separate thread.

-- Sergei.

Apr 21 '06 #5

P: n/a
akrapus wrote:
Thanks for reply.

So would it be implemented as follows:

Func 1
Func 2
Func 3

Thread for Func 1
Thread for Func 2
Thread for Func 3

Cheers

Stevan


Here's how I'm doing it, using the thread module (there's a higher
level class-based module: threading, but I'm more comfortable with the
simple one):

import thread, time

class ThreadDemo:
def __init__(self):
self.data = []
self.lock = thread.allocate_lock()
self.alive = False
def Start(self):
self.alive = True
thread.start_new_thread(self.Process, (None,))
def Stop(self):
self.alive = False
def Process(self, dummy=None):
while self.alive:
self.lock.acquire()
self.data.sort()
self.lock.release()
time.sleep(0.05)
def Add(self, value):
self.lock.acquire()
self.data.append(value)
self.lock.release()
from ThreadDemo import ThreadDemo
demo = ThreadDemo()
demo.Start()
demo.Add(1)
demo.Add(5)
demo.Add(2)
demo.data [1, 2, 5] demo.Add(3)
demo.data [1, 2, 3, 5] demo.Stop()
demo.Add(4)
demo.data

[1, 2, 3, 5, 4]

Use the lock whenever you are going to modify data which is shared
between more than one thread. Using time.sleep stops the thread from
hogging CPU cycles. You only need to sleep a few milliseconds. The
thread will clean itself up as soon as it's function (in this case
Process) finishes. The ugly (None,) tuple in the start_new_thread /
dummy=None Process parameter are because the start_new_thread function
demands a tuple of arguments, even when none are required.
see http://www.python.org/doc/current/li...le-thread.html for more
info.

Iain

Apr 21 '06 #6

P: n/a
Here's a recipe I used a lot:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/65448

Apr 21 '06 #7

P: n/a
akrapus wrote:
Hi,

I am trying to understand how to use threading in Python. I get
threading as a concept, but not the implementation.

In order to start threading, do you call it as a separate function,
which will then be applied to the rest of the code (functions) or do
you open threading in each function. This all can probably be answered
by 'How python threads different functions'?

Hope if somebody can drop me a few lines. I've been trying with
different tutorials, but still do not understand.


It think many Python newcomers have similar questions (and confusion
worries about those other answers).

Maybe thats because they bought the thread/threading.Thread/Queue stuff
and confusion from Java, C++ and that like.( And even encourages that
throw-and-pray .join()

This recipe offers BackgroundCall, which (example) you can probably
understand fuildly within a second:

http://aspn.activestate.com/ASPN/Coo.../Recipe/491280

Thus, you could forget about the technical term "thread".
As you write yoursef "..call it as a separate function..": Thats how
(new) Python programmers think naturally ?
-robert
Apr 21 '06 #8

P: n/a
akrapus wrote:
Hi,

I am trying to understand how to use threading in Python. I get
threading as a concept, but not the implementation.

In order to start threading, do you call it as a separate function,
which will then be applied to the rest of the code (functions) or do
you open threading in each function. This all can probably be answered
by 'How python threads different functions'?

Hope if somebody can drop me a few lines. I've been trying with
different tutorials, but still do not understand.

Cheers,

Stevan

So, I've played with python threading a fair bit, but if you don't need
to delve too far into anything special you can get away with doing
things fairly simply using the threading module

Basically (and this may be over simplifiied for this example), but you
can create a thread() object and basically pass it a function pointer.
Then you can control the thread object fairly easily with methods such
as start() and join().

So, here's an example of how I might thread a function:

#BEGIN Code
def funcToThread():
print "I'm a function"
myThread = threading.Thread(target=functToThread) #a function name
without parens basically yields a function pointer
myThread.start() #Performs necessary tasks in order to run this thread

#Here might be other stuff in your main thread of execution

myThread.join() #say you want to force synchronization at some point

#END Code

Anyhow, there are more ways to do it and it really depends on your needs
as to how far you need to take this.

Hope that helps.

-carl

--

Carl J. Van Arsdall
cv*********@mvista.com
Build and Release
MontaVista Software

Apr 21 '06 #9

P: n/a
Thanks a lot to all of you guys.
I think it'll take some time before I grasp it properly...

Apr 21 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.