RSS/Atom feed Twitter
Site is read-only, email is disabled

Concurrency in Python plug-ins

This discussion is connected to the gimp-user-list.gnome.org mailing list which is provided by the GIMP developers and not related to gimpusers.com.

This is a read-only list on gimpusers.com so this discussion thread is read-only, too.

2 of 3 messages available
Toggle history

Please log in to manage your subscriptions.

Concurrency in Python plug-ins Tomaž Šolc 15 Sep 09:18
  CAH0mxTQco4NvV81-ZYfBf43ra=... 15 Sep 18:50
   Concurrency in Python plug-ins Tomaž Šolc 15 Sep 18:49
Tomaž Šolc
2018-09-15 09:18:11 UTC (over 5 years ago)

Concurrency in Python plug-ins

Hi everyone,

if I assign a keyboard shortcut to a plug-in function it seems that GIMP will sometimes execute two instances of the function concurrently. I've noticed that in my onion layers plug-in: if I press the shortcut key faster than the function takes to execute I get the "Plug-in left image undo in inconsistent state" dialog. Some debug print statements in my Python code seem to confirm that two instances of my function were running at the same time.

I'm wondering if it's possible to prevent that. I've tried having a global with Python's threading.Lock but it doesn't seem to work (I'm guessing two instances are executed in different interpreter contexts?)

Thanks for your help, Tomaž

Tomaž Šolc
2018-09-15 18:49:27 UTC (over 5 years ago)

Concurrency in Python plug-ins

Hi

Thanks a lot for your quick reply.

I looked into using parasites for locking, but I didn't find a good way. attach_new_parasite() seems to return successfully even if a parasite with the same name exists, so I can only do parasite_find() and then attach which still leaves a race condition between these two calls.

So I used file locking with fcntl. It seems to work, but it's Linux only as far as I know.

Here's my current solution if someone else encounters the same problem:

LOCK_DIR = os.environ.get('XDG_CACHE_HOME', os.path.join(os.environ['HOME'], '.cache')) LOCK_FILE = os.path.join(LOCK_DIR, 'gimp-plugin-onion-layers-lock')

@contextmanager def flocked():
with open(LOCK_FILE, "w") as fd:
try:
fcntl.flock(fd, fcntl.LOCK_EX)
yield
finally:
fcntl.flock(fd, fcntl.LOCK_UN)

Best regards Tomaž

On 15. 09. 2018 16:31, Joao S. O. Bueno wrote:

Plugin s are run in separate processes. I am on the mobile now, but the quick to type suggestion is to make your plugin put a "lock" Marci, either as a parasite on the image, or in a fixed name temporary file on the filesystem.

On Sat, Sep 15, 2018, 06:18 Tomaž Šolc > wrote:

Hi everyone,

if I assign a keyboard shortcut to a plug-in function it seems that GIMP will sometimes execute two instances of the function concurrently. I've noticed that in my onion layers plug-in: if I press the shortcut key faster than the function takes to execute I get the "Plug-in left image undo in inconsistent state" dialog. Some debug print statements in my Python code seem to confirm that two instances of my function were running at the same time.

I'm wondering if it's possible to prevent that. I've tried having a global with Python's threading.Lock but it doesn't seem to work (I'm guessing two instances are executed in different interpreter contexts?)

Thanks for your help, Tomaž

_______________________________________________ gimp-user-list mailing list
List address:    gimp-user-list@gnome.org
List membership: https://mail.gnome.org/mailman/listinfo/gimp-user-list List archives:   https://mail.gnome.org/archives/gimp-user-list