Isotropix Forums

Race Conditions

General Discussion about Isotropix and CG related topics

Race Conditions

Unread postby nmoreno » Tue Jun 02, 2020 10:07 pm

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
    Arguments:
        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
    ix.selection.select(item_path)
    ix.application.copy()

    # Select where the item will be pasted, and paste
    ix.set_current_context(dest_path)
    ix.selection.select(dest_path)
    print "About to paste"
    ix.application.paste()
    print "Paste Finished"

    # Put the selection back where it was
    ix.set_current_context(str(ctx))
    print "Done copy and paste"


Output:
Code: Select all
Copy()
About to paste
Paste Finished
Done copy and paste
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!
nmoreno
 
Posts: 5
Joined: Fri Mar 27, 2020 3:09 am

Re: Race Conditions

Unread postby dcourtois » Wed Jun 03, 2020 2:10 pm

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

ix.application.paste()
ix.application.wait_for_events()
User avatar
dcourtois
 
Posts: 95
Joined: Tue Jul 25, 2017 3:15 pm


Return to General Discussion