Maya Locking on json.load()

So we were running in to an issue yesterday where for some reason we were having maya lock up on a lot of artists while running this one tool. We traced it and after a while found out that it was locking when we were reading a json file using json.load()

I ended up just switching it to read to get the text from the file, then just run json.loads(), and it appears that fixed it, but I’m not sure why. I was thinking at first it was because the function didn’t close the file, but then I was closing it right after the load run. If you look at the code, load literally just reads the file object and runs loads with the text from the file. So in summary:

 # Worked
f = open( os.path.join(pm.internalVar(upd=True), '%s_materials.json' % self.currentProject) )
txt = f.read()
f.close()
dict = json.loads(txt)

# Jammed (sometimes)
f = open( os.path.join(pm.internalVar(upd=True), '%s_materials.json' % self.currentProject) )
dict = json.load(f)
f.close()

Anyone else ever run in to load doing that? I’m glad it’s fixed, but annoyed at that I have no clue why that fixed it :sigh:

As a good habit and maybe as a solution to your problem, have you tried using a context manager for the file operation? While I’ve never encountered an issue surrounding this, you may want to specify the open mode as being “r” for read, (though “r” is the default value for open.)


import json 

with open ("yourfile.json", "r") as f:
    result = json.load(f)
    print result

Are there any other processes acting on this file that could be locking it from being read at the same time the json load at tempts to read? Another silly but thorough debugging step would be to move your file path parsing logic into the line above open() to ensure it really is your open() call that’s causing the error. Also, have you eliminated your json data as a source of error? Try json.load on other mock data.

Yeah, that’s a bad habit I need to kick. Nothing else should have it open at that point, but I could give that a shot, see if that does it. One of the bigger problems is that it was so hard to re-produce. It would just sometimes crash after a few times, then other times, immediately the first time you try it in a Maya session and sometimes changing behavior on the exact same task. One of those things that usually works like a charm when you’re stepping through in a remote debugger, but shows its ass when you skip through, but we were finally able to track it to that.

It is running a many times in a short span of time, so it might be some kind of overload thing, even though in that case, reading the text beforehand should be doing the same thing. We’re experimenting with trying to use a project material browser tool with our FBX exporter to try and auto-hookup to materials in Unity, so it was kind of a re-purposed method. But yeah, I could probably try rewrite a bit to make it run a read a little less often.

And this was happening on other computers with different data as well. It might be possible that the tool that’s making the JSON files is botching something, but since loads() works fine, I’m not sure how I could test that much.