Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple non-blocking sleep in twisted.
def sleep(secs):
d = Deferred()
reactor.callLater(secs, d.callback, None)
return d
@forrestv

This comment has been minimized.

Copy link

commented Jan 24, 2018

Thanks!

@baby5

This comment has been minimized.

Copy link

commented Sep 27, 2018

python3 use case with simple 'sleep' implemention

from twisted.internet.defer import inlineCallbacks
from twisted.internet import reactor
from twisted.internet.task import deferLater

def sleep(secs):
    return deferLater(reactor, secs, lambda: None)

@inlineCallbacks
def f():
    print('writing for 5 seconds ...')
    yield sleep(5)
    print('now i am back ...')

f()

reactor.callLater(6, reactor.stop)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.