threads and sleep?

  • Thread starter Jeffrey Maitland
  • Start date
P

Peter Hansen

Jonathan said:
Your sarcasm is cute, I suppose, but think about it for a minute. If
the opposite of what I assert is true, why would even the mainstream
press be running articles along the lines of "multicore CPUs mean
programming will get tougher because locking is hard to get right and
you can't just scale by relying on the cpu to run your one
thread/process really fast anymore."

http://www.gotw.ca/publications/concurrency-ddj.htm for one example.

Thanks, but then let me remove the last (sarcastic) part, the "as you
appear to be", and just leave the rest as is. I am *not* familiar with
this issue, but nevertheless still feel that the OP's problem does not
involve any such locking, so although it's quite possible that you are
correct, I have nothing more to add on the matter. If you think he
needs locking, and that therefore multi-process stuff via Pyro might not
work, he's probably the guy to talk to... I happen to feel it would
probably work fine.

-Peter
 
P

Peter Hansen

Grant said:
That in a multi-theraded Python program, the code that
impliments the Python VM is executing "simultaneously" in
multiple contexts: one for each thread (and possibly one master
thread).

Okay, this description seems correct. Multiple threads, multiple
stacks, therefore multiple contexts and yes, by this definition,
multiple interpreters.

No "master thread" however. Just all the threads that are visible in
"threading.enumerate()", which includes the main thread (that the code
runs in at startup) and any new ones spawned afterwards.

-Peter
 
G

Grant Edwards

Okay, this description seems correct. Multiple threads,
multiple stacks, therefore multiple contexts and yes, by this
definition, multiple interpreters.

No "master thread" however. Just all the threads that are
visible in "threading.enumerate()", which includes the main
thread (that the code runs in at startup) and any new ones
spawned afterwards.

Oh. I assumed that CPython used Posix threads on Posix
platforms. At least in my experience under Linux, libpthread
always creates an extra "manager" thread. Though in our case
that thread probably wouldn't be running a Python interpreter.
 
A

Andreas Kostyrka

Am Dienstag, den 05.07.2005, 08:37 -0700 schrieb Jonathan Ellis:
In many ways, Python is an incredibly bad choice for deeply
multithreaded applications. One big problem is the global interpreter
lock; no matter how many CPUs you have, only one will run python code
at a time. (Many people who don't run on multiple CPUs anyway try to
wave this off as a non-problem, or at least worth the tradeoff in terms
of a simpler C API, but with multicore processors coming from every
direction I think the "let's pretend we don't have a problem" approach
may not work much longer.)
Well, it's not a tradeoff in a simpler C API. It's a more complicated
thing. Much more complicated. ;) Basically nobody has been able to
propose a sensible solution for removing the GIL.

Any solution would have to have the following properties:

a) must be safe.
b) should be probably not slow as a snail ;)

The problem with a) is that loosing this property is not a proposition.
As it is probably a core benefit of Python. So the ceval function would
have to lock any object used it encounters when executing. Trouble: How
to deal with deadlocks. And it would entail locking and unlocking heaps
of objects on any line of Python code.

Basically the current state of art in "threading" programming doesn't
include a safe model. General threading programming is unsafe at the
moment, and there's nothing to do about that. It requires the developer
to carefully add any needed locking by hand. Any error in doing that
will give very hard to debug errors that might show up only in very
specific hardware configurations. And there is no way to detect these
errors automatically, which would be needed to raise a nice exception,
which is the standard at the moment in Python.
If the GIL isn't an issue (and in your case it clearly is), you'll
quickly find that there's little support for debugging multithreaded
applications, and even less for profiling.

As I've said above, there is a case that the current "safe computing"
model of Python isn't compatible with the current state of art in
threading.
Sometimes running multiple processes is an acceptable workaround; if
not, good luck with the rewrite in Java or something else with real
thread support. (IIRC Jython doesn't have a GIL; that might be an
option too.)

Jython might not have a GIL, but it probably will be have really bad
performance because it has to lock all kind of objects during
executation.
Python is a great tool but if you really need good threading support
you will have to look elsewhere.

Yes and no. For a completely general threading support, Python isn't
probably what one wants. OTOH general threading developement is a bit
more painful than many application developers want to endure. There are
ways to do a more structured threading in Python quite ok:

a) rewrite your number crunching thread stuff in C and release the GIL.
b) if you've got a task that can live with less communication -> one
might fork of some computation and communicate the results via pipe.
c) Live with the GIL.

While multi-core CPUs are coming, it will a time before the mainstream
hardware will get to more than 2 logical CPUs. You get quite a bit of
speedup already by delegating all the OS and other background tasks to
one CPU core.

And one thing one shouldn't forget is that finely multithreaded apps
aren't faster magically. If you spend need to add 50% more work for
locking, you will not get many benefits from threading on a 2 core box:

Assuming a two-core box does have about the processing power of 1.8
cores (because of memory contentation, and other problems. Depending
upon use and the exact hardware it might be even worse than that).

Now your single-threaded app runs 10seconds.
With locking this would be about 15seconds.
15 seconds divided by 1.8 gives 8.33seconds.

And that assumes that your application is perfectly threadable and will
have no contentation for data between it's threads. And it assumes a
favorable 1.8 speedup factor for 2 cores. And the hardware level
overhead for more cores goes up, especially if you run multiple threads
of one program -> because the fine interaction between this threads
raises the contentation for data between the cores/processors.

So, yes Python isn't multithreading well. At least not at the moment.
But this is basically there isn't currently a theoretical way to provide
the environment that Python does safely in a multithreaded app without
an incredible performance hit.

Andreas

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBC1MzxHJdudm4KnO0RApfHAKDfv0dS85IsWtryE1eD769kWJ/uOACgmfC9
s7GA2Mfe3Hax6leuMXvvpaA=
=UA5+
-----END PGP SIGNATURE-----
 
A

Andreas Kostyrka

Am Donnerstag, den 07.07.2005, 22:56 +0000 schrieb Grant Edwards:
Oh. I assumed that CPython used Posix threads on Posix It does.
platforms. At least in my experience under Linux, libpthread
always creates an extra "manager" thread. Though in our case
It probably does. But it will probably not show as a Python thread.
Without some special interfacing with the Python/C API any thread
created in C will not show up in Python.
that thread probably wouldn't be running a Python interpreter.

Andreas

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBC1M3pHJdudm4KnO0RAjdOAJ0c/jw0Qb6Nk6HtQqvikPWoo9dTWgCgtXxj
Px9oRVOdZ4o9r1dfihvJC3Y=
=USOk
-----END PGP SIGNATURE-----
 
A

Andreas Kostyrka

Am Mittwoch, den 06.07.2005, 04:00 +0000 schrieb Dennis Lee Bieber:
{I'm going to louse up the message tracking here by pasting part of
your
follow-up into one response}

2> Upon further thought, that just can't be the case. There has
2> to be multiple instances of the intepreter because the
2> interpreter can make C system calls that block (thus blocking
2> that instance of the interpreter). Other Python threads within
2> the program continue to run, so there must be multiple Python
2> intepreters.

From the documentation:

"""
The lock is also released and reacquired around potentially blocking
I/O
operations like reading or writing a file, so that other threads can
run
while the thread that requests the I/O is waiting for the I/O
operation
to complete.
"""

It will take someone who's actually worked on the runtime
interpreter, or studied the code, to, uhm, "interpret" all the above
tidbits...

Not really, it's quite trivial. Anything that touches the Python/C API
needs the GIL.

[Python] <--Python/C API --> [Python module in C] <--some API--> [legacy C code]

Now a well behaved Python C module does release the GIL before doing
anything that might block/take a long time.

The drawback in a Python with threading support that runs just one
thread is the additional locking overhead. OTOH it's only done for
operatations that will probably take a long time anyway. (And long is a
relative term here *g*).

Andreas

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBC1M8QHJdudm4KnO0RAttXAJ9lwBoK1Qt0UNlnR6BKRGPQzUGLLwCgobXq
1roene7WUotvUa1YQPQLGQs=
=0dz8
-----END PGP SIGNATURE-----
 
A

Andreas Kostyrka

Am Mittwoch, den 06.07.2005, 14:38 +0000 schrieb Grant Edwards:
Unfortunately that means you've got to debug a number cruncher
that's written in C.

If one is careful, one can use Pyrex :)

Andreas

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBC1M9kHJdudm4KnO0RAnneAKCSNGqYG+PjnYjihhP3bq3/3ymg9QCfdMVr
kis8pFHVZLExZJVKXpKTNS0=
=cF1L
-----END PGP SIGNATURE-----
 
A

Andreas Kostyrka

Am Mittwoch, den 06.07.2005, 12:27 -0700 schrieb Jonathan Ellis:
Your sarcasm is cute, I suppose, but think about it for a minute. If
the opposite of what I assert is true, why would even the mainstream
press be running articles along the lines of "multicore CPUs mean
programming will get tougher because locking is hard to get right and

Easy ;)

They run these articles, because

-) they have copied it from the press releases.

-) it's partly true.

-) it's the current hype in processors.


That doesn't change the contrary facts:

-) the general threading programming model is very hard to get right.
It's basically at the moment where we were with memory management at C
level. Painful, and errorprone. Nothing to be happy about.

-) There is a spectrum of problems from "requires to be run in sequence"
to can be run on as many "work slaves via the internet". Fact is that
Python is bad at a certain slice (can benefit from tightly
interoperating threads). And that will not change that quickly, because
there are certain problems. But Python works quite well for the
neighboring segments, and it got a number of solutions for a number of
problems in this segment. (Be it Twisted, forking, threads + GIL, etc.)

Andreas

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBC1Oc9HJdudm4KnO0RAimgAKC4Ajg8z0648bSrTZCHojKv7tRCKQCdHGj8
a0JEbF57Ftwj4oOPcPHti2A=
=en2+
-----END PGP SIGNATURE-----
 
M

Mike Meyer

Andreas Kostyrka said:
That doesn't change the contrary facts:

-) the general threading programming model is very hard to get right.
It's basically at the moment where we were with memory management at C
level. Painful, and errorprone. Nothing to be happy about.

I'd say the common threading programming model is very hard to get
right. There are other models that are easy to get right. They've
been around for 20+ years. The problem is, they didn't make it into
any popular languages, so you're stuck with the model that you talk
about. Or maybe you're aware of those, and are saying all of those
models aren't "right" in some way?

People say good things about using the Queue module for Python. I
haven't investigated it thoroughly, so I have no idea how general it
is.

<mike
 
P

Paul Rubin

Andreas Kostyrka said:
Basically the current state of art in "threading" programming doesn't
include a safe model. General threading programming is unsafe at the
moment, and there's nothing to do about that. It requires the developer
to carefully add any needed locking by hand.

So how does Java do it? Declaring some objects and functions to be
synchronized seems to be enough, I thought.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top