Concurrency in Python plug-ins

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Concurrency in Python plug-ins

Tomaž Šolc
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:    [hidden email]
List membership: https://mail.gnome.org/mailman/listinfo/gimp-user-list
List archives:   https://mail.gnome.org/archives/gimp-user-list

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Concurrency in Python plug-ins

Tomaž Šolc
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 <[hidden email]
> <mailto:[hidden email]>> 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:    [hidden email]
>     <mailto:[hidden email]>
>     List membership: https://mail.gnome.org/mailman/listinfo/gimp-user-list
>     List archives:   https://mail.gnome.org/archives/gimp-user-list
>


_______________________________________________
gimp-user-list mailing list
List address:    [hidden email]
List membership: https://mail.gnome.org/mailman/listinfo/gimp-user-list
List archives:   https://mail.gnome.org/archives/gimp-user-list

signature.asc (849 bytes) Download Attachment