473,545 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python threads on multi-CPU machines

If I have a dual-processor hyperthreaded machine (so with four CPU
contexts), will a python program distribute threads over all four
logical processors?

I ask because I'm fairly sure that this *does* happen using the
threading extensions in MFC, and fairly sure that it *doesn't* when
using Java, so I don't see that the result is obvious for python.

Tom
Jul 18 '05 #1
6 7428
Thomas Womack <tw*****@chiark .greenend.org.u k> wrote in
news:vQ*******@ news.chiark.gre enend.org.uk:
If I have a dual-processor hyperthreaded machine (so with four CPU
contexts), will a python program distribute threads over all four
logical processors?

I ask because I'm fairly sure that this *does* happen using the
threading extensions in MFC, and fairly sure that it *doesn't* when
using Java, so I don't see that the result is obvious for python.


The C implementation of Python uses a global interpreter lock that only
allows one thread to interpret bytecode at a time, so while the threads may
be distributed across multiple processors you will get little or no speedup
over a single processor. (If your threads spend most of their time in a
non-Python extension, they may be able to get some benefit from multiple
processors).

The only way to take advantage of multiple processors with Python is to run
at least one separate process for each processor. For example, I believe
Zope will take advantage of multiple processor systems if you run it in ZEO
client/server mode with several Zope client processes.

I believe that Jython simply does whatever the Java implementation does.

--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #2
Thomas Womack:
If I have a dual-processor hyperthreaded machine (so with four CPU
contexts), will a python program distribute threads over all four
logical processors?


Yes, unless you are CPU bound in the Python interpreter.
(CPU bound in an extension is fine, and I/O bound, as when
doing network is also fine.)

For a longer answer, see back threads concerning the global
interpreter lock and "free-threading".

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #3
> However, C extensions can release the
GIL, and almost all I/O code in Python does that, so I/O-heavy programs
will make good use of the SMP.


FYI, I'm also working on adding that functionality to the PIL library,
seeing as imaging operations can be fairly expensive too. It makes
these processes more "friendly" to other threads (and Tk) even on
single-CPU boxes, and allows taking advantage of multiple CPUs where
available.

I'll be posting links on the PIL mailing list when it's complete (just
needs exhaustive testing now... it'll be a while), and Fred has agreed
to merge it into the core distribution afterwards.

Kevin.
Jul 18 '05 #4
In article <5a************ **************@ posting.google. com>,
Kevin Cazabon <ke***@cazabon. com> wrote:
Aahz:

However, C extensions can release the GIL, and almost all I/O code
in Python does that, so I/O-heavy programs will make good use of the
SMP.


FYI, I'm also working on adding that functionality to the PIL library,
seeing as imaging operations can be fairly expensive too. It makes
these processes more "friendly" to other threads (and Tk) even on
single-CPU boxes, and allows taking advantage of multiple CPUs where
available.


Cool! When you're done, I'd appreciate it if you could write up a short
summary of the problems you ran into and post it here. I'm interested
in pushing computational threading in Python, but I haven't written
enough C code to have any idea how it differs from other kinds of
threading issues.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Jul 18 '05 #5
Thomas Womack <tw*****@chiark .greenend.org.u k> wrote in message news:<vQ******* @news.chiark.gr eenend.org.uk>. ..
If I have a dual-processor hyperthreaded machine (so with four CPU
contexts), will a python program distribute threads over all four
logical processors?
Okay others told you already about the GIL.
I ask because I'm fairly sure that this *does* happen using the
threading extensions in MFC, and fairly sure that it *doesn't* when
using Java,


Of couse java *does* this. It's the number one reason for SUN to
develop and maintain Java: selling a reason to buy some of there
overpriced multiprocessor machines. Maybe you don't know anything
about Java.
Jul 18 '05 #6
Just one general caveat, based on some multi-thread code I worked on about a
year ago: if you want to support your users running your multithread code on
multi-CPU machines, you *must, must, must* stress test on multi-CPU
machines!

We had a very nasty intermittent bug (collision with threads in Tcl
interpreter) that I could *not* reproduce on my single-processor development
box, but which showed up in the field on the customer's quad-processor
machine.
Finally, after about a day and a half, I moved over to our dual processor
test machine, and the bug showed up after about 20 minutes of stress
testing. It turned out that the 3rd party thread library we were using was
built to be compatible with the Win98 (!) thread API - once I rebuilt to run
using the WinNT thread API, the window was closed, and the app ran great on
the multiCPU servers.

I'm sorry if this is off-topic, and I am fairly new in this newsgroup, but I
know we were very eager to push multiCPU machines on our customers, and we
did not realize what risk we were taking on.

Sincerely,
-- Paul McGuire
Austin, TX

Jul 18 '05 #7

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

Similar topics

1
5821
by: Gernot Hillier | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi! I'm the developer of a Linux ISDN application which uses embedded Python for controlling the communication. It starts several threads (i.e. one for each incoming call and for outgoing faxes) which run Python scripts in embedded Python interpreters which in turn do the real...
0
1834
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to explain it in details, 1. Python initialization and finalization is done in the *main* thread. 2. For each new thread I create a separate...
0
1215
by: Mustafa Demirhan | last post by:
Hello, I am trying to add Python scripting support to one of my applications and I really need some help regarding the thread terminations. The application is a multi-threaded Windows application and multiple threads need to run Python code from time to time. Everything works fine when all the threads operate without any errors. Each...
1
3697
by: amit | last post by:
Hello, I am embedding a python script in a C++ application. The script can be called simultaneously from multiple threads. What is the correct way to implement this situation: 1) Have unique python interpreter instantiations ( Py_Initialize() ) for each thread.
0
7943
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
7456
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...
0
6022
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...
1
5359
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
5076
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
3490
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
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
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.