Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Add Twisted Deferred support #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add Twisted Deferred support #15

wants to merge 2 commits into from

Conversation

hawkowl
Copy link

@hawkowl hawkowl commented Aug 31, 2013

This patch adds Deferred support to time_calls and hist_calls by a wrapper function. Instead of updating the timer or updating with the result upon calling it (which, will be a Deferred, not an actual result), it tacks on a callback which does the updating instead. If the function does not return a Deferred, it calls the callback synchronously and returns the result as expected.

I tested it in a virtualenv which had no Twisted and it worked, although I am not absolutely sure, but Travis should be able to tell us that.

All tests pass here - I'm not 100% sure how I can write tests that will work if Twisted isn't there, since it uses Trial... so I'm not sure if I just need to write some additional tests that assume Twisted is there?

@hawkowl
Copy link
Author

hawkowl commented Aug 31, 2013

Here is the rough script I have been using to test:

"""
Adapted from the yunomi readme, for the purposes of demonstrating deferreds in yunomi
"""

import time
from yunomi import timer, time_calls
from twisted.internet import defer, reactor, threads

TARGET = 100000

def _fib():
    """
    Adapted from http://twistedmatrix.com/documents/12.3.0/core/howto/gendefer.html
    An example of a CPU-heavy operation
    """

    first = 0
    second = 1

    for i in xrange(TARGET - 1):
        new = first + second
        first = second
        second = new

    return second

@time_calls
def fibonnaci_number_deferred(useless):

    print "def fib %s" % time.time()
    return threads.deferToThread(_fib)

@time_calls
def fibonnaci_number_traditional():

    print "fib %s" % time.time()
    return _fib()

for i in xrange(20):
    fibonnaci_number_traditional()

d_chain = fibonnaci_number_deferred("no use")

for i in xrange(20):
    d_chain.addCallback(fibonnaci_number_deferred)

d_chain.addCallback(lambda _a: reactor.stop())
reactor.run()

print "Traditional %s" % timer("fibonnaci_number_traditional_calls").get_sum()
print "Deferred %s " % timer("fibonnaci_number_deferred_calls").get_sum()

With this patch:

Traditional 2.23070454597
Deferred 2.35104727745

With current master:

Traditional 2.190284729
Deferred 0.00170731544495

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant