Git Product home page Git Product logo

deferred's Introduction

Deferred

Python Callbacks & Deferreds

Python implementation of the jQuery Callbacks et Deferred objects

Callbacks

from callbacks import Callbacks

def fn1(v):
  print('fn1 says: %s' % v)
def fn2(v):
  print('fn2 says: %s' % v)

c = Callbacks(memory=True)
c.add(fn1).fire('hello')
c.add(fn2)
c.fire('hi')
fn1 says: hello
fn2 says: hello
fn1 says: hi
fn2 says: hi

Deferred

from threading import Thread
from sys import exc_info
from urllib import request
from deferred import Deferred

deferred = Deferred()

def sendRequest (readFn, deferred):
  try:
		res = readFn()
		deferred.resolve(res)
	except:
		deferrred.reject(exc_info()[1])

try:
	req = request.Request(url='http://thiswebsitedoesnotexistatall.org/')
	kwargs = {'readFn':request.urlopen(req).read,'deferred':deferred}
	Thread(target=sendRequest,kwargs=kwargs).start()
except:
	deferred.reject(exc_info()[1])

deferred.done(lambda res:print('Success !\n%s' % res)).fail(lambda e:print('Fail ! %s' % e))
Fail ! <urlopen error [Errno 11001] getaddrinfo failed>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.