Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Please help with Threading

Thread Tools

Re: Please help with Threading

Cameron Simpson
Posts: n/a
On 20May2013 10:53, Carlos Nepomuceno <(E-Mail Removed)> wrote:
| I just got my hands dirty trying to synchronize Python prints from many threads.
| Sometimes they mess up when printing the newlines.
| I tried several approaches using threading.Lock and Condition.
| None of them worked perfectly and all of them made the code sluggish.

Show us some code, with specific complaints.

Did you try this?

_lock = Lock()

def lprint(*a, **kw):
global _lock
with _lock:
print(*a, **kw)

and use lprint() everywhere?

For generality the lock should be per file: the above hack uses one
lock for any file, so that's going to stall overlapping prints to
different files; inefficient.

There are other things than the above, but at least individual prints will
never overlap. If you have interleaved prints, show us.

| Is there a 100% sure method to make print thread safe? Can it be fast???

Depends on what you mean by "fast". It will be slower than code
with no lock; how much would require measurement.

Cameron Simpson <(E-Mail Removed)>

My own suspicion is that the universe is not only queerer than we suppose,
but queerer than we *can* suppose.
- J.B.S. Haldane "On Being the Right Size"
in the (192 book "Possible Worlds"
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help with Threading Jurgens de Bruin Python 13 06-03-2013 01:47 AM
RE: Please help with Threading Carlos Nepomuceno Python 0 05-20-2013 07:45 AM
Re: Please help with Threading Cameron Simpson Python 0 05-20-2013 07:45 AM
Re: Please help with Threading Cameron Simpson Python 0 05-19-2013 03:10 AM
Help with printing a bit pattern with printf and %x C Programming 9 04-19-2013 12:14 PM