473,583 Members | 3,155 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 1547
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
6698
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,...
19
6456
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...
4
4895
by: Joe Wong | last post by:
Hi, Is there any way to increase/decrease a thread priority in Python? Best regards, -- Wong
0
1522
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
2703
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...
3
1488
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...
17
6403
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...
9
1848
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...
14
3434
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
1215
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...
0
7827
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...
0
8184
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8328
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5701
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...
0
5375
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...
0
3820
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2334
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
0
1158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.