threading, how to?

2006-04-21 Thread akrapus
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading, how to?

2006-04-21 Thread Sergei Organov
"akrapus" <[EMAIL PROTECTED]> 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading, how to?

2006-04-21 Thread akrapus
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading, how to?

2006-04-21 Thread Diez B. Roggisch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading, how to?

2006-04-21 Thread Sergei Organov
"akrapus" <[EMAIL PROTECTED]> 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading, how to?

2006-04-21 Thread Iain King
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/lib/module-thread.html for more
info.

Iain

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading, how to?

2006-04-21 Thread [EMAIL PROTECTED]
Here's a recipe I used a lot:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65448

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading, how to?

2006-04-21 Thread Carl J. Van Arsdall
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
[EMAIL PROTECTED]
Build and Release
MontaVista Software

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading, how to?

2006-04-21 Thread akrapus
Thanks a lot to all of you guys.
I think it'll take some time before I grasp it properly...

-- 
http://mail.python.org/mailman/listinfo/python-list


BackgroundCall as Pythonic Thread - Re: threading, how to?

2006-04-21 Thread robert
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/Cookbook/Python/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
-- 
http://mail.python.org/mailman/listinfo/python-list