[Python-de] Re: Code Style Review

Marc Haber <[email protected]>
Newsgroups gmane.comp.python.general.german
Message-ID <[email protected]>
On Tue, Nov 29, 2022 at 06:51:17PM +0100, Stefan Schwarzer wrote:
> On 2022-11-28 22:07, Marc Haber wrote:
> > Kann ich die Locks innerhalb der entsprechenden Funktion definieren
> > oder bekomme ich dann in jedem Thread ein eigenes Lock, was der
> > Intention entgegen spricht?
> > 
> > Ist:
> > 
> > foolock = threading.Lock()
> > def foo:
> >      with foolock:
> >          (tue Dinge mit foo, potenziell multithreaded)
> > 
> > dasselbe wie
> > 
> > def foo:
> >      foolock = threading.Lock()
> >      with foolock:
> >          (tue Dinge mit foo, potenziell multithreaded)
> > 
> > ?
> 
> Die Ansätze sind _nicht_ gleichwertig. Wie du richtig
> erkannt/vermutet hast, definierst du im zweiten Ansatz bei
> Ausführung von `foo` "on the fly" ein Lock-Objekt.

Dann muss ich hier mit globalen Locks arbeiten?

> > Ist die in
> > https://www.bogotobogo.com/python/Multithread/python_multithreading_Using_Locks_with_statement_Context_Manager.php
> > und
> > https://www.pythontutorial.net/python-concurrency/python-threading-lock/
> > verwendete Schreibweise, wo das Lock als Parameter mit in die Funktion
> > hineingereicht wird, wirklich "schönes" Python?
> 
> Allgemein würde ich nicht `acquire` und `release` explizit
> verwenden beziehungsweise die meisten der Situationen, wo
> man `acquire` und `release` verwendet, können mit einem
> `with some_lock:` übersichtlicher geschrieben werden.

Ja, das ist mir auch sympathischer.

> Ein weiterer Ansatz ist, Queues zur Synchronisation zu
> verwenden, was tendenziell deutlich weniger fehleranfällig
> ist.

Das könnte für die vorliegende Verwendung overkill sein.

Grüße
Marc

-- 
-----------------------------------------------------------------------------
Marc Haber         | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany    |  lose things."    Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature |  How to make an American Quilt | Fax: *49 6224 1600421
_______________________________________________
python-de Mailingliste -- [email protected]
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.