I have created a RFC server as a windows service using PYSAP. Everything is 100%.....my only problem is that when I STOP the service it "hangs".
I have tried killing the current process using the following:
handle = win32api.OpenProcess(1, 0, os.getpid())
....which does actually go from the task manager BUT the service does not stop properly.
I have written out a log file on what is actually happening and it is because the RFC connection is not closing.....
So I looked at the "pysap.py" code and see that it does a WHILE LOOP......so in MY code I do something like the following:
while win32event.WaitForSingleObject(self.hWaitStop, 0) == win32event.WAIT_TIMEOUT:
logger.debug("Entering NT event loop...")
logger.debug("Before main loop...")
logger.debug("After main loop...")
And when I look at the log file AFTER I try STOP to service I can see that it hangs in the "main_loop" above....
2007-09-10 11:49:15,467 - Logos_Log - DEBUG - Initialising logger...
2007-09-10 11:49:15,467 - Logos_Log - DEBUG - Entering NT event loop...
2007-09-10 11:49:15,467 - Logos_Log - DEBUG - Before main loop...
Then I thought I could get a HANDLE on the current RFC connection by doing something like the following:
myargs = ['d:\\rfc_server.py', '-a', 'PY_RFC_SERVER', '-g', '126.96.36.199', '-x', '3300']
BUT that doesn't work at all...the handles would be different(I think)......the "main_loop" in the PYSAP module will not end.....
Here is the "main_loop" code in the PYSAP module....
"""Pass sys.argv as argument to this function."""
raise SapRfcError('connection failed')
if rc==0: # RFC_OK
librfc.RfcAbort(handle,'Function %r not found' % func_name)
elif rc==17: # RFC_SYSTEM_CALLED
elif rc==6: # RFC_CLOSED
else: # Error
raise SapRfcError('RFC server error, rc = %d' % rc)
if err==0: rc=librfc.RfcClose(handle)
Does anyone have an idea how to "kill" the RFC server "elegantly"?
Your help is greatly appreciated!
You will need the latest version of pysap from CVS (head) which supports the listen (non-blocking) and dispatch (blocking) methods in addition to main_loop. Here is a short snippet from my program:
srv = pysap.RfcServer()
# Open a connection (registered RFC server in this case)
handle = srv.connect(['', '-a', 'mcl.controller', '-g', '10.2.1.200', '-x', 'sapgw00'])
# Start infinite loop
rc = win32event.WaitForSingleObject(self.hWaitStop, 500)
# Exit if service is stopped
if rc == win32event.WAIT_OBJECT_0:
# Process single RFC call
# NOTE: the call to listen method is non-blocking i.e. it returns immediately which is preferable
# in this case (otherwise service would exit only after the call was made to the RFC server)
# If you need blocking behaviour use srv.dispatch(handle) instead
Thanks so much for your help.....I downloaded the latest version from CVS and have used the methods as you stated, it works like a dream ;-)
BTW: Just curious to know how you would handle losing a connection to the SAP gateway while the service is running? I would assume you just catch the exception in the loop and carry on trying to connect to the gateway?
Thanks again for your help!