Work Around for os.startfile Crashing arcmap

4686
5
Jump to solution
06-05-2015 07:38 AM
BenjaminMittler
Occasional Contributor III

Hi All,

I'm currently attempting to develop an addin that creates a KML from a layer within arcmap, and then opens that KML. All seems to be going well except that every time the tool runs it crashes arcmap. I am aware that this is because i am using the "os.startfile()" command. What possible workaround can i use to get this file to open at the end of my script.

Thanks,

Ben

*Not a python expert by any means

Tags (1)
1 Solution

Accepted Solutions
KevinBell
Occasional Contributor III

Jason Schriener at ESRI got this work around for me.

Add this def:

def run_in_other_thread(function): #conflicts w/ os.startfile will crash arcmap.  this is a workaround

    # functool.wraps will copy over the docstring and some other metadata

    # from the original function

    @functools.wraps(function)

    def fn_(*args, **kwargs):

        thread = threading.Thread(target=function, args=args, kwargs=kwargs)

        thread.start()

        thread.join()

    return fn_

Call it like this:

startfile = run_in_other_thread(os.startfile)

startfile(theFilePath)

View solution in original post

5 Replies
ToddBlanchette
Occasional Contributor II

Do you want to open the KML in your current MXD in ArcMap? Or using an outside program like Google Earth?

0 Kudos
BenjaminMittler
Occasional Contributor III

Todd,

Google Earth, the file actually opens when my addin is run, it just crashes arcmap in the process of doing so.

Thanks,

Ben

0 Kudos
ToddBlanchette
Occasional Contributor II

Ben,

Thanks for more info.  If you're using Windows, os.startfile() shouldn't be giving you any issues, and like you said, it starts Google Earth fine (if kml/kmz is associated with that program within windows as the default program).  Is there anything else in your code after the os.startfile() is called?

0 Kudos
KevinBell
Occasional Contributor III

Jason Schriener at ESRI got this work around for me.

Add this def:

def run_in_other_thread(function): #conflicts w/ os.startfile will crash arcmap.  this is a workaround

    # functool.wraps will copy over the docstring and some other metadata

    # from the original function

    @functools.wraps(function)

    def fn_(*args, **kwargs):

        thread = threading.Thread(target=function, args=args, kwargs=kwargs)

        thread.start()

        thread.join()

    return fn_

Call it like this:

startfile = run_in_other_thread(os.startfile)

startfile(theFilePath)

BenjaminMittler
Occasional Contributor III

Thanks Kevin, worked like a charm.

I actually came across the thread with this information while researching the problem and it ended up that i was just calling it wrong.

For all others running into this error in the future this seems to be the workaround.