Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How about adding async interfaces to some blocking functions? #878

Closed
taeguk opened this issue Aug 31, 2016 · 6 comments
Closed

How about adding async interfaces to some blocking functions? #878

taeguk opened this issue Aug 31, 2016 · 6 comments

Comments

@taeguk
Copy link

taeguk commented Aug 31, 2016

For example, there is a function called cpu_percent().
Now, this function is just blocking with a interval parameter.
A user must use multi-thread to utilize cpu during the interval.
I want to use a asyncio feature for it.
How about adding async interfaces to some blocking functions?

@giampaolo
Copy link
Owner

cpu_percent() can already be used in a non-blocking manner: just specify interval=0 or interval=None (which is the default BTW). The first time it is called it will return a meaningless 0.0 value.

@taeguk
Copy link
Author

taeguk commented Aug 31, 2016

I know what you say.
I explain a need of async when a user want to use cpu_percent() with interval > 0.0.

@giampaolo
Copy link
Owner

from the doc:

When interval is > 0.0 compares system CPU times elapsed before and after the interval (blocking). When interval is 0.0 or None compares system CPU times elapsed since last call or module import, returning immediately (non-blocking).

@giampaolo
Copy link
Owner

@giampaolo
Copy link
Owner

There is no need to change cpu_percent() so that it works with the new
async keyword because if you specify timeout=0 it already returns
immediately. It is already non-blocking / asynchronous, trust me. =)

On Wed, Aug 31, 2016 at 3:06 PM, Taeguk Kwon notifications@github.com
wrote:

async cpu_percent(interval = None, _args, *_kwargs):
psutil.cpu_percent(_args, *_kwargs)
await asyncio.sleep(interval)


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#878 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAplLF2-4UtJCkXh80sz4DFgQ-a9Plnsks5qlXxGgaJpZM4JxkW8
.

Giampaolo - http://grodola.blogspot.com

@taeguk
Copy link
Author

taeguk commented Aug 31, 2016

async def cpu_percent(interval = None, *args, **kwargs):
    if interval is not None and interval > 0.0:
        psutil.cpu_percent(*args, **kwargs)
        await asyncio.sleep(interval)
    return psutil.cpu_percent(*args, **kwargs)

Okay, I got it. I can do my hope through the above codes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants