Skip to content
/ pyyield Public

Simple C++ ::std::this_thread::yield() call for Python, when sleep() calls have unknown effects.

License

Notifications You must be signed in to change notification settings

Tirine/pyyield

Repository files navigation

pyyield: For when you don't know what time.sleep() does.

Keeping it at simple as possible, pyyield is a simple Python module build from C++ and executes 1 line of code:

::std::this_thread::yield()

The only reason for creating this was due to the inconsistencies when using time.sleep(), which until recently was not very well documented to how it's supposed to work when using with 0 or a small float. Where some implementations don't seem to do anything on 0 and other implementations round up very small floats.

That is not to say the C++ yield is always the same either, but for that, read more here

Usage:

Example usage, yield from a loop

from pyyield import pyyield

def workerLoop():
    while performSomeWork():
        pyyield()

Install:

PyPI

pip install pyyield

Build and install from source:

pip install -r requirements.txt
python -m build -o ./dist
pip install ./dist/pyyield*.whl

Tests

pip install pytest
pytest --verbose --log-cli-level=DEBUG

The tests are mainly there to make an extremely simple check of performance differences. Please change the tests and play around with it yourself to understand how pyyield and time.sleep() performs on the system you want to run it on!

For example, as of writing this, in GitHub Actions, ubuntu-latest, pyyield does not perform any better than sleep(0) on all python versions tested, printing the info:

Failed to at speed test: pyyield is not faster than sleep(0)!

About

Simple C++ ::std::this_thread::yield() call for Python, when sleep() calls have unknown effects.

Resources

License

Stars

Watchers

Forks

Packages

No packages published