Significance of yield sleep in AutoBahn twisted code

62 views
Skip to first unread message

Chaitanya Chilukoori

unread,
Jan 5, 2016, 12:02:16 PM1/5/16
to Autobahn
Hi,

I am new to AutoBahn/crossbar/twisted and trying to write a RPC progressive call. In the below code, why yield sleep(1) is needed? If I remove that, client is getting all the responses in one chunk instead of progressive. What does yield sleep(1) do for progressive calls? 

My understanding was details.progress() takes care of progressive. Can you please explain?



@inlineCallbacks
        def longop(n, details=None):
            if details.progress:
                # caller can (and requested to) consume progressive results
                for i in range(n):
                    details.progress(i)
                    yield sleep(1)
            else:
                # process like a normal call (not producing progressive results)
                yield sleep(1 * n)
            returnValue(n)

        yield self.register(longop, 'com.myapp.longop', RegisterOptions(details_arg='details'))


Thanks,
Chaitanya.

Elvis Stansvik

unread,
Jan 5, 2016, 1:03:55 PM1/5/16
to Autobahn
Hi Chaitanya,

2016-01-05 18:02 GMT+01:00 Chaitanya Chilukoori <sree...@gmail.com>:
> Hi,
>
> I am new to AutoBahn/crossbar/twisted and trying to write a RPC progressive
> call. In the below code, why yield sleep(1) is needed? If I remove that,
> client is getting all the responses in one chunk instead of progressive.
> What does yield sleep(1) do for progressive calls?
>
> My understanding was details.progress() takes care of progressive. Can you
> please explain?

The sleep is not needed. In the example it's simply a way to simulate
an operation that takes a long time.

If you remove the sleep, then the for loop will iterate as fast as
your CPU can process it, which wouldn't be a very good example of
progressive calls.

Hope that clears things up. So the sleep is just something that was
put in the example to illustrate. In a real application it would be
some async operation that takes some time.

Elvis

>
>
>
> @inlineCallbacks
> def longop(n, details=None):
> if details.progress:
> # caller can (and requested to) consume progressive results
> for i in range(n):
> details.progress(i)
> yield sleep(1)
> else:
> # process like a normal call (not producing progressive
> results)
> yield sleep(1 * n)
> returnValue(n)
>
> yield self.register(longop, 'com.myapp.longop',
> RegisterOptions(details_arg='details'))
>
>
>
> Thanks,
>
> Chaitanya.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Autobahn" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to autobahnws+...@googlegroups.com.
> To post to this group, send email to autob...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/autobahnws/a426b0d0-b3d0-4fc3-9cda-15e6a9e7f8e1%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Chaitanya Chilukoori

unread,
Jan 5, 2016, 4:03:29 PM1/5/16
to Autobahn
Thank you. That helps.
Reply all
Reply to author
Forward
0 new messages