Please help with Threading

Carlos Nepomuceno carlosnepomuceno at outlook.com
Mon May 20 06:01:13 EDT 2013


----------------------------------------
> Date: Mon, 20 May 2013 18:35:20 +1000
> From: cs at zip.com.au
> To: carlosnepomuceno at outlook.com
> CC: python-list at python.org
> Subject: Re: Please help with Threading
>
> On 20May2013 10:53, Carlos Nepomuceno <carlosnepomuceno at outlook.com> 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?


It works! Think I was running the wrong script...

Anyway, the suggestion you've made is the third and latest attempt that I've tried to synchronize the print outputs from the threads.

I've also used:

### 1st approach ###
lock  = threading.Lock()
[...]
try:
    lock.acquire()
    [thread protected code]
finally:
    lock.release()


### 2nd approach ###
cond  = threading.Condition()
[...]
try:
    [thread protected code]
    with cond:
        print '[...]'


### 3rd approach ###
from __future__ import print_function

def safe_print(*args, **kwargs):
    global print_lock
    with print_lock:
        print(*args, **kwargs)
[...]
try:
    [thread protected code]
    safe_print('[...]')



Except for the first one all kind of have the same performance. The 
problem was I placed the acquire/release around the whole code block, 
instead of only the print statements.

Thanks a lot! ;)

> 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.
>
> Cheers,
> --
> Cameron Simpson <cs at zip.com.au>
>
> 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 (1928) book "Possible Worlds" 		 	   		  


More information about the Python-list mailing list