Hi everyone . I'm going to write a windows service in python 27. There is a built-in function that mosaic picture tiles named arcpy.mosaicmanagement() . I want to give the function to a thread to work simultaneously. I have two problems:
1- I could run the program as a service in Windows, and everything is working pretty good and I see the service in Services but the win32 service being stopped after starting one time.
2- How could I get the method to several threads to work simultaneously
here is the code I have tried.
What I have tried:
import os,fnmatch
import glob
import arcpy
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import logging
import os.path
from arcpy import env
from arcpy.sa import *
logging.basicConfig(
filename = 'e:\\Temp\\PictureService.log',
level = logging.DEBUG,
format = '[PictureGenerator-service] %(levelname)-7.7s %(message)s'
)
class PictureGeneratorSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "PictureGenerator-Service"
_svc_display_name_ = "PictureGenerator Service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.stop_event = win32event.CreateEvent(None,0,0,None)
#socket.setdefaulttimeout(420)
self.stop_requested = False
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.stop_event)
logging.info('Stopping service ...')
self.stop_requested = True
def SvcDoRun(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,'')
)
self.main()
def main(self):
arrFiles = []
#i=0
strPath = 'E:\\Zargari\\FRECRaster Dataset\\DrG-GoogleDownload - Copy\\MyTests\\ReadyToImport\\'
logging.info(' ** Picture Generator ** ')
# Simulate a main loop
if self.stop_requested:
logging.info('A stop signal was received: Breaking main loop ...')
#time.sleep(60)
logging.info("Service Started at %s" % time.ctime())
if not os.listdir(strPath) :
for root, dirs, filenames in os.walk(strPath):
for file in filenames:
if file.endswith(('.tif')):
arrFiles.append(file)
try:
#while i<len(arrFiles):
for i in range(len(arrFiles)):
thread = threading.Thread(target=arcpy.Mosaic_management,args=(strPath + arrFiles[i], "E:\Zargari\FRECRaster Dataset\MyTests\MyTests\Z.gdb\\FREC", "LAST", "FIRST", "", "", "NONE", "0", "NONE"))
thread.setDaemon(true)
thread_list.append(thread)
thread.start()
#arcpy.Mosaic_management(inputs=strPath + arrFiles[i], target="E:\Zargari\FRECRaster Dataset\DrG-GoogleDownload - Copy\MyTests\Z.gdb\\FREC", mosaic_type="LAST", colormap="FIRST", background_value="", nodata_value="", onebit_to_eightbit="NONE", mosaicking_tolerance="0", MatchingMethod="NONE")
#i+=1
logging.info("Exception occurred %s" % arrFiles[i] % time.ctime())
time.sleep(60)
except exception as e:
logging.info("Exception occurred %s" % e % time.ctime())
return
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(PictureGeneratorSvc)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(PictureGeneratorSvc)