Are threads bad? - was: Future of Pypy?

Chris Angelico rosuav at gmail.com
Tue Feb 24 00:57:00 EST 2015


On Tue, Feb 24, 2015 at 4:27 PM, Paul Rubin <no.email at nospam.invalid> wrote:
>> Sure, your code might not be making any mutations (that you know of),
>> but malloc definitely is [1], and that's just the tip of the iceberg.
>> Other things like buffers for stdin and stdout, DNS resolution etc.
>> all have the same issue.
>
> Re stdin/stdout: obviously you can't have
> multiple threads messing with the same fd's; that's the same thing as
> data sharing.

Actually, you can quite happily have multiple threads messing with the
underlying file descriptors, that's not a problem. (Though you will
tend to get interleaved output. But if you always produce output in
single blocks of text that each contain one line with a trailing
newline, you should see interleaved lines that are each individually
correct. I'm also not sure of any sane way to multiplex stdin -
merging output from multiple threads is fine, but dividing input
between multiple threads is messy.) The problem is *buffers* for stdin
and stdout, where you have to be absolutely sure that you're not
trampling all over another thread's data structures. If you unbuffer
your output, it's probably going to be thread-safe.

ChrisA



More information about the Python-list mailing list