473,388 Members | 1,326 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,388 software developers and data experts.

Python Threading

Hello,
Can anyone explain the main points in working with threads in Python.
Why use threading and not Thread.I have read an article that i have to
subclass the Thread class and override some function.

Sep 20 '06 #1
4 1635
On 20 Sep 2006 00:27:07 -0700, daniel <da***********@gmail.comwrote:
Hello,
Can anyone explain the main points in working with threads in Python.
Why use threading and not Thread.I have read an article that i have to
subclass the Thread class and override some function.
I repeat this all the time, but the best advice I can give you about
using threads is to not use threads at all. I would point you to good
references like Threads Considered Harmful
(http://www.kuro5hin.org/story/2002/11/18/22112/860) and The Problem
with Threads (http://www.eecs.berkeley.edu/Pubs/Te...CS-2006-1.html
- With Link to PDF). It might seem an inappropriate response to your
question to simply tell you that you should not do what you are asking
how to do, but its just the case that most often anyone without
exposure to threads has little or no understanding on just how bad
they are for many of the tasks they will be used for. Threads are
difficult to control, impossible to predict, and simply one of the
most over used, least understood causes of buggy, unmaintainable
software in the whole spectrum of development techniques.

As alternatives, look into what tasks can spawn into other processes,
asyncronous programming (a'la Twisted -
http://www.twistedmatrix.com/), and co-routine and similar facilities,
such as the tasklets of Stackless and two-way generators now included
with Python.
Sep 21 '06 #2
Dennis Lee Bieber wrote:
... slightly
simpler to use without subclassing (at the simplest level, you just pass
the function that is to be run, and a list of the arguments/parameters
it needs, to the threading creation class).
that's exactly how thread.start_new_thread(func, args) works, of course,
so I'm not sure that's much of an argument.

</F>

Sep 21 '06 #3
Calvin Spealman wrote:
I repeat this all the time, but the best advice I can give you about
using threads is to not use threads at all.
Might as well get with the times and ignore that advice.
I would point you to good
references like Threads Considered Harmful
(http://www.kuro5hin.org/story/2002/11/18/22112/860)
Note that it's "poster boy" for multiple processes
now uses multiple threads.
and The Problem
with Threads
(http://www.eecs.berkeley.edu/Pubs/Te...CS-2006-1.html
- With Link to PDF).
The author's alternatives to threads are not available
in Python. Many Pythoners do similar things with threads,
such as use the message-passing style via the queue module.
It might seem an inappropriate response to your
question to simply tell you that you should not do what you are asking
how to do, but its just the case that most often anyone without
exposure to threads has little or no understanding on just how bad
they are for many of the tasks they will be used for. Threads are
difficult to control, impossible to predict, and simply one of the
most over used, least understood causes of buggy, unmaintainable
software in the whole spectrum of development techniques.
Threads require some study and understanding, but they work
great once one learns to use them.
As alternatives, look into what tasks can spawn into other processes,
Unfortunately one cannot share Python objects between processes.
POSH may change that, but it looks to be stuck in alpha.
asyncronous programming (a'la Twisted -
http://www.twistedmatrix.com/),
I find asynchronous programming much harder to manage than
threads, except for fairly simple applications where neither
one is hard.
and co-routine and similar facilities,
such as the tasklets of Stackless and two-way generators now included
with Python.
Those fail at simple things such as having two of them each
waiting at a blocking call.
--
--Bryan
Sep 22 '06 #4
daniel wrote:
Can anyone explain the main points in working with threads in Python.
They are system threads, so the main points are much like in
other languages. Examine how thread.lock works, and look up the
queue module.
Why use threading and not Thread.
There's no reason not to use thread, but threading adds
some useful items, such as setDaemon(), condition variables,
semaphores, and maybe rlocks. Try to avoid using the timeout
argument on condition variables if efficiency matters; Event and
queue.queue are implemented on top of condition variables, so
same deal.
I have read an article that i have to
subclass the Thread class and override some function.
You can, but that's the hard way. The following snippet runs
func(arg1, arg2) in a daemon thread:

import threading
# ...

t = threading.Thread(target=func, args=(arg1, arg2))
t.setDaemon(True)
t.start()
--
--Bryan
Sep 22 '06 #5

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

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
7
by: kanzen | last post by:
I keep telling my friends that Python rocks. Now it's time to put my money where my mouth is. I'm about to start writing a server for a phone based game. It needs to handle simlpe requests from...
9
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
0
by: Ling | last post by:
I am using boost.python to wrap C++ function which includes directmusic libraries to simply play the midi, but lots of linkage errors "error LNK2001: unresolved external symbol". I wonder if it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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 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.