473,781 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*********@mvi sta.com
Build and Release
MontaVista Software

Dec 13 '05 #1
5 1556
In article <ma************ *************** ************@py thon.org>,
Carl J. Van Arsdall <cv*********@mv ista.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**@pythoncra ft.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*********@mvi sta.com
Build and Release
MontaVista Software

Dec 14 '05 #3
Il 2005-12-14, Carl J. Van Arsdall <cv*********@mv ista.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************ *************** ************@py thon.org>,
Carl J. Van Arsdall <cv*********@mv ista.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**@pythoncra ft.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**@pythoncra ft.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
6761
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 language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace, dynamic typing, and that it is interpreted - not compiled. These three put python under fire and cause...
19
6487
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. "somethread.interrupt()" will wake somethread up when it's in sleeping/waiting state. Is there any way of doing this with python's thread? I suppose thread interrupt is a very primitive functionality for stopping a blocked thread.
4
4907
by: Joe Wong | last post by:
Hi, Is there any way to increase/decrease a thread priority in Python? Best regards, -- Wong
0
1529
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 python 2.3.2-1
13
2718
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 Contest is scheduled on Sunday, 27 Feb 2005. This is the first Online Programming Contest in India to support Python !!!!. Other languages supported are C and C++.
3
1494
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 killed when the import of the module is complete. Is this expected? Does python have to be in control to allow threads to run? Would it be better to arrange things such that the file is processed using PyRun_SimpleFile? David S. Harrison
17
6433
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. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this problem. The operating system is MS Windows XP Professional.
9
1864
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 CPU-intensive work in another thread, but of course this won't work because of Python's GIL. There's a lot of past discussion on this, and I want to bring it up again because with the work on Python 3000, I think it is worth trying
14
3459
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 exclusion, and make results deterministic. For this purpose, and inspired by Ruby(1.9) fiber, I wrote my own version of fiber in Python.
0
1226
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 arbitrary thread. (When the signal module is available, interrupts always go to the main thread.) i.e., all threads (including main) to catch interrupt exceptions, and propagate that information to other threads. 2. since there is no way to interrupt...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10076
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.