Frage

Ich habe ein Skript in Python, die eine Ressource verwendet, das nicht mehr als eine bestimmte Menge von gleichzeitigen Ausführen von Skripts verwendet werden kann.

Classically, würde dies durch eine benannte Semaphore gelöst werden, aber ich kann nicht die in der Dokumentation der Multiprozessing Modul oder Threading .

fehlt Bin ich etwas oder sind benannt Semaphore nicht implementiert / ausgesetzt von Python? und was noch wichtiger ist, wenn die Antwort nein ist, was ist die beste Art und Weise zu emulieren ein?

Danke, Boaz

PS. Aus Gründen, die auf diese Frage nicht so relevant sind, kann ich nicht die Aufgabe einen kontinuierlich laufenden Prozess / Daemon oder Arbeit mit gelaicht Prozessen aggregieren - beide, wie es scheint, hätte mit dem Python-API gearbeitet.

War es hilfreich?

Lösung

I suggest a third party extension like these, ideally the posix_ipc one -- see in particular the sempahore section in the docs.

These modules are mostly about exposing the "system V IPC" (including semaphores) in a unixy way, but at least one of them (posix_ipc specifically) is claimed to work with Cygwin on Windows (I haven't verified that claim). There are some documented limitations on FreeBSD 7.2 and Mac OSX 10.5, so take care if those platforms are important to you.

Andere Tipps

You can emulate them by using the filesystem instead of a kernel path (named semaphores are implemented this way on some platforms anyhow). You'll have to implement sem_[open|wait|post|unlink] yourself, but it ought to be relatively trivial to do so. Your synchronization overhead might be significant (depending on how often you have to fiddle with the semaphore in your app), so you might want to initialize a ramdisk when you launch your process in which to store named semaphores.

Alternatively if you're not comfortable rolling your own, you could probably wrap boost::interprocess::named_semaphore (docs here) in a simple extension module.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top