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

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 7418
Thomas Womack <tw*****@chiark.greenend.org.uk> wrote in
news:vQ*******@news.chiark.greenend.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.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\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***@dalkescientific.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**@pythoncraft.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.uk> wrote in message news:<vQ*******@news.chiark.greenend.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
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....
0
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...
0
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.