Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Comparison of callback and generator approaches:
- Callback approach:
- def onSomeEvent(self, data):
- setup stuff
- call-some-API(data, self.success_callback, self.error_callback)
- def success_callback(self, data):
- do stuff with data (as result)
- tart.send('response', data)
- do another API call with more callbacks...
- def error_callback(self, data):
- handle the error case
- tart.send('blarg')
- x10 for pain level
- Generator approach:
- def onSomeEvent(self, data):
- setup stuff
- try:
- result = yield ApiCallObject(data)
- except ApiCallError:
- handle the error case
- tart.send('blarg')
- return
- do stuff with result
- tart.send('response', result)
- do another API call, in-line without callbacks...
- even nice things like: yield TimeDelay(seconds=1.5)
- or fancier stuff like:
- try:
- yield Timeout(seconds=1.0)
- yield ApiCall()
- except TimeoutError:
- oops, the operation didn't complete in time
- except ApiCallError:
- oops, the operation failed
- else:
- yay! everything worked and the timeout is cancelled automatically
Advertisement
Add Comment
Please, Sign In to add comment