[Twisted-Python] how to cancel a long running job on a server ?

Thomas Jung Thomas.Jung at iisb.fraunhofer.de
Tue May 10 06:26:38 EDT 2005


Hello,

another newbie question:

I have a server, where I can remotely call a function like remote_calc(), 
using the perspective broker,
which returns giving a result.
Sometimes, I want to cancel that computation, calling e.g. remote_cancel() 
(which remains to be implemented.....)

How can I have remote_calc() not to return before the result is done, and 
still be able to call remote_cancel while its running ?

I tried this:
=============================================================

from twisted.spread import pb
from twisted.internet import reactor,defer,threads

@defer.deferredGenerator
def calc(adeferred):
    print "this is calc..."
    d=defer.waitForDeferred(adeferred)
    yield d
    result=d.getResult()
    print "calc: result=",result
    return


def do_calc():
    print "do_calc starting..."
    for i in range(10000):
        for j in range(10000):
            pass
    print "do_calc done..."
    return 456


class ServerObject(pb.Root):

    def remote_cancel(self):
        print "this is server::cancel..."

    def remote_calc(self,result):
        print "server::calc: ..."

       # thought this would wait until done, but doesnt....

        calc(threads.deferToThread(do_calc))
        print "server::calc: returning"
        return 123


if __name__ == '__main__':
    factory = pb.PBServerFactory(ServerObject())
    reactor.listenTCP(8800, factory)
    reactor.run()

===============================================================================

My expectation was that the call to "calc" in remote_calc would block, but it 
does not.
And if I dont defer the calculation to a thread, remote_cancel is not done 
until calculation finishes ...

Maybe there is some completely different way to do this in twisted ?

Thanks for any hint !



-- 
Dr. Thomas Jung
Fraunhofer-Institut IISB
91058 Erlangen, Schottkystr. 10
+49 9131 761264




More information about the Twisted-Python mailing list