Page 1 of 1

Race Conditions

Unread postPosted: Tue Jun 02, 2020 10:07 pm
by nmoreno
I ran into a weird race condition when writing a copy and paste function (I just really need a copy of the object placed in a different context) and was wondering if there is a way to enforce order. We worry that this sort of thing may appear when calling other functions and would like to know how to avoid these race conditions or enforce the order. It doesn't always happen, but it happens often, and causes other code to fail because the object that it expected, does not exist yet and is needed to continue. I tried waiting for it in a loop but ended up hanging, I tried using is_protected but it didn't make much sense on how I would use it here. I got around it by just creating another object with the fields that I needed copied over one by one for specific objects. But I am still wondering what is wrong with the code below, which ideally would copy anything, does it fail because it's interacting with the UI? Should I be using different functions to copy objects?

I put the code below, and the output that I kept getting. Note, Copy() and Paste() are printed by Clarisse and not my code.

Code: Select all
def copy_and_paste(ix, item_path, dest_path):
    """Copy and paste and item in Clarisse
        ix: The Clarisse api
        item_path (str): The path to the item to copy
        dest_path (str): The path to where to put the copy
    # Get the current context to set back when done
    ctx = str(ix.get_current_context())

    # Select the item to copy, and copy

    # Select where the item will be pasted, and paste
    print "About to paste"
    print "Paste Finished"

    # Put the selection back where it was
    print "Done copy and paste"

Code: Select all
About to paste
Paste Finished
Done copy and paste

That paste is done way after I need it, and not where it should be happening, which is between "About to Paste" and "Paste Finished".
Thanks in advance!

Re: Race Conditions

Unread postPosted: Wed Jun 03, 2020 2:10 pm
by dcourtois
Probably because copy/paste stuff are commands, and commands are only processed during the application's idle time, so basically only after your script is done running.

Try to add the following after the paste() call:

python code