python-hyper / hyper

HTTP/2 for Python.
http://hyper.rtfd.org/en/latest/
MIT License
1.05k stars 192 forks source link

Callback support #245

Closed yichengchen closed 8 years ago

yichengchen commented 8 years ago

I'am a beginner of Python learning. I wonder It's there any way to make a requests callback in http/2 such as requests-futures?

def bg_cb(sess, resp):

parse the json storing the result on the response object

resp.data = resp.json()

future = session.get('http://httpbin.org/get', background_callback=bg_cb)

It would be great for me to sending and receiving message at the same time. Actually I'm trying to make a simple Apple APNS server using new http/2 Api,But I don't know how to handle things like this. qq20160526-0

Or is there a better idea to achieve this? Thank you.

Lukasa commented 8 years ago

Hey there! Thanks for raising this issue. =)

So, in principle you should be able to mount the requests adapter provided by hyper inside the Session provided by requests-futures. However, I've never actually tried it, so you'd need to investigate.

However, the latest version of hyper is thread-safe, so if you did that you should find no specific problems.

As to APNS, what you want to do depends on whether you're working on a client or a server. If you want a client, this library is fine. If you want a server, this library won't suit: it's only a client. You'd need to go down to a lower-level library, like hyper-h2.

yichengchen commented 8 years ago

Thank you,that way is working. But I wonder how could i know it's using connection with streams or not.i doubt that requests-futures would close the connection after it finish single request.

By the way I'm working on a APNS client....that a mistake.LOL

Lukasa commented 8 years ago

@yichengchen The adapter takes care of this: it does "connection pooling" by having just a single connection per (scheme, host, port) tuple. That means that each subsequent request nonetheless goes to the same adapter.

yichengchen commented 8 years ago

Thanks!