Skip to main content

Python extensions by alkasm

Project description

pykasm

A collection of useful utilities and utility libraries for my projects.

Install

pip install pykasm-alkasm

Usage

TimeoutIterable

A timeout will not help with indefinite blocking. Concurrent futures will attempt to cancel the pull on the generator, if possible.

from pykasm import TimeoutIterable
from time import sleep

def tired_generator(n):
    for seconds in range(1, n+1):
        sleep(seconds)
        yield f"slept for {seconds} second(s)"

# will timeout if more than 3 seconds elapse between iterations
for v in TimeoutIterable(tired_generator(5), timeout=3.14):
    print(v)
sleeping for 1 second(s)
sleeping for 2 second(s)
sleeping for 3 second(s)

StreamBuffer

The stream buffer is not particularly robust.

  • Indefinite blocking will cause the thread to also indefinitely block.
  • Generators that tax the CPU heavily may have a hard time shutting off.
  • Use with caution, and not in production.
from pykasm import StreamBuffer
from itertools import count
from time import sleep

# collect values from infinite generator `count()` over a period of time
sleep_time = 0.0001
with StreamBuffer(count()) as buf:
    time.sleep(sleep_time)
    counts = list(buf.flush())
print(f"collected {len(counts)} values in {sleep_time} seconds!")
collected 13647 values in 0.0001 seconds!

@timed

You'll need to set logging level to DEBUG to see the timer messages.

from pykasm import timed
from time import sleep
import logging
logging.basicConfig(level=logging.DEBUG)

@timed
def takes_a_second():
    time.sleep(1)

takes_a_second()
DEBUG:root:takes_a_second took 1004.352 ms

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pykasm-alkasm, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size pykasm_alkasm-0.1.2-py3-none-any.whl (3.0 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size pykasm-alkasm-0.1.2.tar.gz (2.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page