python - Twisted PerspectiveBroker callRemote from wsgi webapp -



python - Twisted PerspectiveBroker callRemote from wsgi webapp -

i'm writing wsgi application needs utilize twisted perspectivebroker phone call remote methods. problem wsgi needs homecoming rendered webpage, calls twisted service asynchronous. webapp needs phone call remote methods, other stuff, has wait remote calls finish, render page , homecoming client. best way this?

i planning on using flask write app.

a wsgi application runs in own thread (or process). when running in twisted's wsgi container, different thread reactor running in. of twisted's apis not thread-safe: may called in reactor thread.

so, basic way phone call twisted api wsgi application using reactor.callfromthread, thread-safe , causes function called in reactor thread:

... reactor.callfromthread(pbremote.callremote, "somemethod", some, args)

however, discards result, you want. it's simple build api on top of reactor.callfromthread preserves result, though, , there's implementation of in twisted, too:

from twisted.internet.threads import blockingcallfromthread ... result = blockingcallfromthread(reactor, pbremote.callremote, "somemethod", some, args)

this phone call block until deferred returned callremote fires , homecoming result of deferred.

if want create call, other work, , wait phone call finish, have little bit creative. need create phone call , actual deferred returns, not block on it:

resultholder = blockingcallfromthread( reactor, lambda: [pbremote.callremote("somemethod", some, args)])

and can other work need do. , when you're ready wait result of pb call:

result = blockingcallfromthread(reactor, lambda: resultholder[0])

this rather more awkward using twisted in single-threaded scenario, may indeed easier utilize twisted web's native apis rather build wsgi application. remember 1 of primary goals of wsgi allow applications developed portable across different servers - twisted, apache, etc. if you're using twisted apis in wsgi application, it's not portable @ all.

python twisted wsgi

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -