geertj / gruvi

Async IO for Python, Simplified

Home Page:http://gruvi.readthedocs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example of using switchback with futures? ..Or propose alternate way to accomplish?

brizzbane opened this issue · comments

commented

Love what you made. I've been wanting to figure this out for a while, but have been hesitant to ask.

Can you help me with this.., not sure if I'm understanding how to use get_hub().switch() correctly.

Here is an example of what I'm trying to accomplish..

def prepare_handle(self, ph):
    print('prepare handle self.users %s' % self.users)

    def handle_response(future, user):
        print('future %s' % future)


    request = cURLHTTPRequest('http://google.com')
    future = self.client.req(request)

         #   gruvi.get_hub().switch()
    with gruvi.switch_back(timeout=10) as switcher:
        print('here!!!')
        sigh = pyuv.Signal(gruvi.get_hub().loop)
        sigh.start(switcher, pyuv.Signal().SIGHUP)
        future.add_done_callback(functools.partial(switcher, future, 'asdf'))
        switcher.add_cleanup(sigh.close)
        gruvi.get_hub().switch()

So combining Fibers & (ultimately) results of the values of Futures. Didn't see any examples using gruvi.get_hub().switch() AND switchback.

MUCH appreciated.

What is cURLHTTPRequest? I assume this is an external library? Unless that is specifically written or configured to work with Gruvi it is not going to work. It would need to use Gruvi and pyuv to implement cooperative IO.

Your options are to either run this external library use blocking IO and run it in a thread pool (gruvi.get_io_pool()), or use gruvi.http.HttpRequest. A third option that sometimes works is if cURLHTTPRequest supports asynchronous IO callbacks is to use those to call into Gruvi for async IO (typically using get_hub().poll.

commented

If you read my reply (previous, deleted)--trying to condense and clarify.

It's a custom pycurl client I made..specifically trying to integrate with gruvi and pyuv, because I love eventloops.

client.req() returns a future. the request WORKS as in its processed, but when I try to 'read' the result, gruvi complains that I can not view .result() from the main hub.

So, yes, its specifically for gruvi. My question I guess is how can I read the future's result? Does it need to be 'inside' a switchback, or..?

commented

I think I may just be confused, heh. Right now, my issue when putting a request into a Fiber, and get_hub().switch(), is that if Im trying to do multiple requests, it just stops after the first (i.e. loop is still running, but doesn't go on to next request).

ugh. Im going to mess around w/things for a few more days so I don't waste your time. I kind of didn't look at the Poller docs before.

commented

OK.

https://gist.github.com/brizzbane/b799ef86fa73ea119308

@ the bottom main. Only the first item in the list proceeds. Am I misunderstanding something fundamental?

if __name__ == '__main__':
    ioloop = gruvi.get_hub().loop
    c = cURLSMTPClient(ioloop)
    users = ['henry', 'tom', 'terrry', 'joejoe', 'ashley', 'sara', 'jessi']

    for u in users:
        r = cURLSMTPRequest(url='smtp://mail.mailinator.com/',
                                      mail_from='timmyjones1@gmail.com',
                                      mail_rcpt='%s@bobmail.info' % u,
                                      message='hi')

        future = gruvi.Fiber(c.req, (r,))
        future.start()
        gruvi.get_hub().switch()

I'm wanting to use pycurl + pyuv, because I also believe it to be best available implementation of an eventloop. Your library and rationale really strike a cord w/me, so wanting to understand how to work w/it properly (and read the result of futures).

If you have the time to help, would be much appreciated. If you find value w/any of the pycurl + implementation I have, feel free to grab it and publish. There is still a lot of code in above gist from different ways I've tried implementing handling file descriptor events.

Hi @brizzbane ,

not sure if you are still interested in this given that you reported this a long time ago. Recently I have gotten some free time again to work on Gruvi.

If you're still trying to get this to work, please share a preferably small and standalone reproducer. Happy to have a look.