Parallel(?) programming with python

Cameron Simpson cs at cskk.id.au
Mon Aug 8 18:37:34 EDT 2022


On 08Aug2022 11:20, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>Andreas Croci <andrea.croci at gmx.de> writes:
>>Basically the question boils down to wether it is possible to have parts
>>of a program (could be functions) that keep doing their job while other
>>parts do something else on the same data, and what is the best way to do
>>this.
>
>  Yes, but this is difficult. If you ask this question here,
>  you might not be ready for this.

This is a very standard requirement for any concurrent activity and the 
typical approach is a mutex (mutual exclusion).  You've already hit on 
the "standard" approach: a `threading.Lock` object.

>        lock.acquire()
>        try:
>            list.append( i )
>        finally:
>            lock.release()

Small note, which makes writing this much clearer. Lock objects are 
context managers. So:

    with lock:
        list.append(i)

is all you need.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list