I would like to run a multiprocessing task from a python add-in tool. My issue is that the process keeps failing. Basically crashes ArcMap.
Here is my basic code:
def function(startOID, endOID, fc):
wrksp = r"c:\temp\mp_addintest\data\test_%s.txt" % (int(startOID) + int(endOID))
# real logic removed to dumb it down
with open(wrksp, 'w') as writer:
writer.write("%s to %s from %s \n" % (startOID, endOID, fc))
return wrksp
class btnMP(object):
"""Implementation for src_addin.MPButton (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
pool = None
try:
pythonExe = os.path.join(sys.exec_prefix, 'python.exe')
multiprocessing.set_executable(pythonExe)
pool = multiprocessing.Pool(4)
results = []
for i in xrange(4):
results.append(pool.apply_async(function, [str(1),
str(i),
str("test")]))
pool.close()
pool.join()
for result in results:
print result.get()
except:
del pool
print 'error'
If I run the code outside of ArcMap or from a toolbox, it works without a problem, but when I put the logic inside a button, it causes arcmap to crash.
My guess is that ArcMap is running in process for all python add-ins. Is there a work around for this issue?
I've tried adding in the freeze_support() to the code as well, but that did nothing as well.
-
1If ArcMap is crashing, contact your ESRI support. if they can replicate it they'll acknowledge its a bug (and maybe even fix it one day).GIS-Jonathan– GIS-Jonathan2012年10月31日 10:40:03 +00:00Commented Oct 31, 2012 at 10:40
-
Have you installed all 5 Service Packs for ArcGIS 10.x that are already out? Maybe that'll helpSergios Kolios– Sergios Kolios2012年11月02日 16:07:51 +00:00Commented Nov 2, 2012 at 16:07
-
The OP is using 10.1Petr Krebs– Petr Krebs2012年11月02日 16:14:08 +00:00Commented Nov 2, 2012 at 16:14
-
Also service packs are cumulative so you only need to install the latest one, not each in succession.blah238– blah2382012年11月02日 20:23:59 +00:00Commented Nov 2, 2012 at 20:23
-
SP1 for 10.1 was released this week.Timothy Michael– Timothy Michael2012年11月02日 21:58:45 +00:00Commented Nov 2, 2012 at 21:58
1 Answer 1
Parallel processing is easier 'shown than done.' In the case of stuffing this all into a button, I'm guessing two issues:
- Multiple threads block the ArcMap UI thread, or
- ArcMap puts its own schema lock on the data source and doesn't permit the python process access to the data.
Hmm looking further issue has been documented here in an ArcGIS Resources page. Schema lock looks like the culprit.
-
Not sure if you meant to link something other than what you did (an ArcGIS forums post, not an official document).blah238– blah2382012年11月01日 06:57:18 +00:00Commented Nov 1, 2012 at 6:57
-
The forum is the correct link. When someone finds more official documentation they can feel free to post it.WolfOdrade– WolfOdrade2012年11月01日 18:07:47 +00:00Commented Nov 1, 2012 at 18:07
-
Thank you for your suggestions. I believe it's actually caused by #1. The thread blocks for ArcMap UI. I am use an SDE database, so schema locks are not my issues here.code base 5000– code base 50002012年11月05日 11:37:16 +00:00Commented Nov 5, 2012 at 11:37
Explore related questions
See similar questions with these tags.