Skip to content

Instantly share code, notes, and snippets.

@nramirezuy
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nramirezuy/14055275449bc4a08fad to your computer and use it in GitHub Desktop.
Save nramirezuy/14055275449bc4a08fad to your computer and use it in GitHub Desktop.
from pprint import pprint
from time import sleep, time
from twisted.internet import defer, reactor
def stop():
if not reactor.getDelayedCalls():
reactor.stop()
reactor.callLater(0.1, stop)
def locked_print(msg):
sleep(0.01)
pprint(msg + ', Time: {}'.format(time()))
def print_later(delay, msg):
dfd = defer.Deferred()
dfd.addCallback(locked_print)
reactor.callLater(delay, dfd.callback, msg)
if __name__ == '__main__':
total = 10
for idx in range(total):
print_later(0.1, 'Delay: 0.1, Index: {}'.format(idx))
for idx in range(total):
print_later(0, 'Delay: 0, Index: {}'.format(idx))
sleep(0.1)
reactor.callLater(0.1, stop)
reactor.run()
'Delay: 0, Index: 0, Time: 1426595265.61'
'Delay: 0.1, Index: 0, Time: 1426595265.62'
'Delay: 0.1, Index: 1, Time: 1426595265.63'
'Delay: 0.1, Index: 2, Time: 1426595265.64'
'Delay: 0.1, Index: 3, Time: 1426595265.65'
'Delay: 0.1, Index: 4, Time: 1426595265.66'
'Delay: 0.1, Index: 5, Time: 1426595265.67'
'Delay: 0.1, Index: 6, Time: 1426595265.68'
'Delay: 0.1, Index: 7, Time: 1426595265.69'
'Delay: 0.1, Index: 8, Time: 1426595265.7'
'Delay: 0.1, Index: 9, Time: 1426595265.71'
'Delay: 0, Index: 1, Time: 1426595265.72'
'Delay: 0, Index: 2, Time: 1426595265.73'
'Delay: 0, Index: 3, Time: 1426595265.74'
'Delay: 0, Index: 4, Time: 1426595265.75'
'Delay: 0, Index: 5, Time: 1426595265.76'
'Delay: 0, Index: 6, Time: 1426595265.77'
'Delay: 0, Index: 7, Time: 1426595265.78'
'Delay: 0, Index: 8, Time: 1426595265.8'
'Delay: 0, Index: 9, Time: 1426595265.81'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment