pyepics / newportxps

python support code for NewportXPS drivers
BSD 2-Clause "Simplified" License
20 stars 17 forks source link

Abort movement but busy socket (XPS_D8) #18

Open PierreJom opened 1 year ago

PierreJom commented 1 year ago

Hi, first of all thank you for you're package, that is super useful All i want is to press a button on my interface that's stop the axis to create like an software emergency button and also to avoid crash (with telemeter laser measurement). But I have an issue with the fact the Abort function does anything. During an axis movement the socket is busy so the function Abort can't send the message to the XPS. Is that possible to open several sockets ?

newville commented 1 year ago

@PierreJom yeah, I have had some trouble with this too. What I find works most reliably is to create a separate NewportXPS instance within a sub-process, and issue an abort_group there.

from multiprocessing import Process
from newportxps import NewportXPS

def create_xps_abort(host, confdict):
    thisxps = NewportXPS(host, **confdict)
    return Process(target=thisxps.abort_group)

Then, in the long-process running and responding to events (like a push of an Abort button), I do

if abort_requested:
    abort_process = create_xps_abort(host, xpsconf) #  xpsconf = {'username': 'Administrator', ....}
    abort_process.start()
    time.sleep(1.0)
    abort_process.join(5.0)
    if abort_process.is_alive():
           abort_process.terminate()
           time.sleep(1.0)
   abort_requested = False

In the not-too-distant past, I had a second "observer process" running that did a couple of other things, but also had a NewportXPS instance, and that would look abort requests and an issue "abort_group()", even though I also had a main "data collection process to run NewportXPS trajectories" process running on the same machine.

I swear that worked for years. But then started seeing problems with the main program sometimes failing to get gathering data for a trajectory scan. When I removed that second process keeping a long-running connection to the XPS , the gathering problem went away, so I switched to this "create a short-lived sub-process and use that to issue the abort" method. That seems to work better....