[Twisted-Python] twisted eats the error

steven meiers commercials24 at yahoo.de
Mon Feb 13 11:00:58 MST 2017


> Sorry, your code example and your explanation don’t make clear what
> you expect to happen, and when.  When you say “the first method that
> is called from my code”, are you referring to  aSillyBlockingMethod,
> or runEverySecond?  In the deferToThread case, you are calling the
> former; in the task.LoopingCall case, you are calling the latter.

Ah youre right, i forgot to write about the most important part.
if the datetime line is wrong you get 
Unhandled Error 

i was looking for a way to get the original error message that:
print(datetime.now().strftime('%H:%M:%S'))

throws if used outside of twisted code.

i know how to correct the code but i would like to know why i dont get
that error on the console.

> If your intent is to trigger the strftime exception as a consequence
> of your call to deferToThread, then you would need to either add
> runEverySecond to the callback chain, or move the call to strftime
> into the aSillyBlockingMethod call.
> 
> Example:
> 
> # this will "sleep" for x seconds
> d = threads.deferToThread(aSillyBlockingMethod, 'some argument’)
> d.addCallback(runEverySecond)
> d.addCallback(printResult)
> d.addErrback(printError)
> 

i tried that one, code looks like this. but i dont get the error
message...still Unhandled error in Deferred

from twisted.internet import reactor, task, threads
import datetime

def aSillyBlockingMethod(x):
    import time
    time.sleep(4)
    return x


def runEverySecond(ignored):

    print(datetime.now().strftime('%H:%M:%S'))    #WRONG, ERROR:
AttributeError: 'module' object has no attribute 'now'
    # that error gets eaten by twisted....why?
    #print(datetime.datetime.now().strftime('%H:%M:%S'))    #WORKS


def printResult(result):
    print("printResult:", result)

def printError(failure):
    print(failure)


# this will "sleep" for x seconds
d = threads.deferToThread(aSillyBlockingMethod, 'some argument')
d.addCallback(runEverySecond)
d.addCallback(printResult)
d.addErrback(printError)

# but this will still run, not getting blocked
l = task.LoopingCall(runEverySecond, 'lala')
l.start(1.0)

reactor.callLater(5, reactor.stop)
reactor.run()





More information about the Twisted-Python mailing list