473,407 Members | 2,359 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,407 software developers and data experts.

Threading in python

Hi everyone, I'm trying to use the threading module with python 2.2 and
I have some questions regarding python's threading.

1. Who schedules which threads run and when? Is this something left up
to the operating system or does python provide a mechanism for this?

2. I've read that python threads don't like to allow other threads to
run except in certain situations, these would be situations where there
is sleep or I/O happening, is this true? If so, what are the cases in
which a python thread would not give up the processor?

3. Is there a way to which thread is running? I mean something a bit
more robust than a print statement inside the thread, I want to be able
to see when a context switch occurs, is this possible? The reason for
this is I will have threads deadlocked waiting for I/O and I am
interested to see how often context switching occurs.

Thanks in advance,

-carl

--

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

Dec 13 '05 #1
5 1540
In article <ma***************************************@python. org>,
Carl J. Van Arsdall <cv*********@mvista.com> wrote:

I have some questions regarding python's threading.
These answers assume you're using CPython; Jython and IronPython have
different answers.
1. Who schedules which threads run and when? Is this something left up
to the operating system or does python provide a mechanism for this?
Strictly OS -- Python provides no control.
2. I've read that python threads don't like to allow other threads to
run except in certain situations, these would be situations where there
is sleep or I/O happening, is this true? If so, what are the cases in
which a python thread would not give up the processor?
There is only a single thread of Python code running at any time.
Calling out to external libraries (e.g. C code) can release the Global
Interpreter Lock. Python's standard I/O routines do this automatically
for you, but you're free to write your own code that does this (e.g.
mxODBC).
3. Is there a way to which thread is running? I mean something a bit
more robust than a print statement inside the thread, I want to be able
to see when a context switch occurs, is this possible? The reason for
this is I will have threads deadlocked waiting for I/O and I am
interested to see how often context switching occurs.


You'd have to write some kind of logging. You can use the logging
module.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Don't listen to schmucks on USENET when making legal decisions. Hire
yourself a competent schmuck." --USENET schmuck (aka Robert Kern)
Dec 14 '05 #2
These answers assume you're using CPython; Jython and IronPython have
different answers.

This confuses me. What is CPython versus Jython and IronPython? I'm
using compiled source from python.org, would this be CPython?

1. Who schedules which threads run and when? Is this something left up
to the operating system or does python provide a mechanism for this?


Strictly OS -- Python provides no control.


I did some research yesterday, and I'd like some clarification. So the
OS handles which thread runs and when, however if one of the python
processes currently holds the global interpreter lock, when the OS
switches to a python thread that does not have this lock, this thread
will do nothing. Does this sound right? Ultimately python does
control what thread runs by controlling the global interpreter lock
although its the underlying OS that handles all the context switching etc.

Because of this global interpreter lock does this mean its impossible to
get speed up with threading on multiple processor systems? I would
think so because only one python thread can execute at any one time. Is
there a way to get around this? This isn't something I need to do, I'm
just curious at this point.

-c
--

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

Dec 14 '05 #3
Il 2005-12-14, Carl J. Van Arsdall <cv*********@mvista.com> ha scritto:
This confuses me. What is CPython versus Jython and IronPython? I'm
using compiled source from python.org, would this be CPython?


CPython is the official distribution, the one you find on python.org
It's C and Python based.

Jython is the Python implementation targetting the JVM

IronPython is the Python implementation targetting the .NET CLR

--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Dec 14 '05 #4
[BTW, please follow standard Usenet convention and attribute the quotes;
I've added them back in for you]

In article <ma***************************************@python. org>,
Carl J. Van Arsdall <cv*********@mvista.com> wrote:
Aahz:
Carl Van Arsdall:

1. Who schedules which threads run and when? Is this something left up
to the operating system or does python provide a mechanism for this?
Strictly OS -- Python provides no control.


I did some research yesterday, and I'd like some clarification. So the
OS handles which thread runs and when, however if one of the python
processes currently holds the global interpreter lock, when the OS
switches to a python thread that does not have this lock, this thread
will do nothing. Does this sound right? Ultimately python does
control what thread runs by controlling the global interpreter lock
although its the underlying OS that handles all the context switching
etc.


No. Python simply uses a standard OS thread lock. When a Python thread
gives up the GIL, the OS decides which thread acquires the lock -- it
could even be the same thread that released the lock.
Because of this global interpreter lock does this mean its impossible to
get speed up with threading on multiple processor systems? I would
think so because only one python thread can execute at any one time. Is
there a way to get around this? This isn't something I need to do, I'm
just curious at this point.


You either need to run multiple processes or run code that mostly calls
into C libraries that release the GIL. For example, a threaded spider
scales nicely on SMP.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Don't listen to schmucks on USENET when making legal decisions. Hire
yourself a competent schmuck." --USENET schmuck (aka Robert Kern)
Dec 14 '05 #5
Jean-Paul Calderone wrote:
On 14 Dec 2005 10:15:08 -0800, Aahz <aa**@pythoncraft.com> wrote:
You either need to run multiple processes or run code that mostly calls
into C libraries that release the GIL. For example, a threaded spider
scales nicely on SMP.


Yes. Nearly as well as a single-threaded spider ;)


I'm confused about how a single-threaded spider -- even using Twisted!
-- could "scale well" on a multiprocessor machine (that's what SMP
means, right?) without using multiple processes. I believe that's the
scenario to which Aahz was referring.

-Peter

Dec 14 '05 #6

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....
4
by: Joe Wong | last post by:
Hi, Is there any way to increase/decrease a thread priority in Python? Best regards, -- Wong
0
by: Jacek Trzmiel | last post by:
Hi, I have a problem with using urllib2 with threading module under Cygwin. $ cygcheck -cd cygwin python Cygwin Package Information Package Version cygwin 1.5.5-1...
13
by: Varun | last post by:
Hi Friends, Department of Information Technology, Madras Institute of Technology, Anna University, India is conducting a technical symposium, Samhita. As a part of samhita, an Online Programming...
3
by: David Harrison | last post by:
I am working on an application on Mac OS X that calls out to python via PyImport_ImportModule(). I find that if the imported module creates and starts a python thread, the thread seems to be...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
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...
14
by: Akihiro KAYAMA | last post by:
Hi all. I found cooperative multi-threading(only one thread runs at once, explicit thread switching) is useful for writing some simulators. With it, I'm able to be free from annoying mutual...
0
by: Edwin.Madari | last post by:
1. check out the Caveats for thread module: http://docs.python.org/lib/module-thread.html Threads interact strangely with interrupts: the KeyboardInterrupt exceptionwill be received by an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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...
0
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,...
0
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...

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.