Limit Concurrency in AsyncIO

You can run multiple async tasks simultaneously in Python using the asyncio native library. This allows you to create and manage asynchronous tasks, such as coroutines, and run them concurrently. Let’s have the following async method, that counts to the length of the given name and returns the name. import asyncio from typing import Awaitable async def count_word(name: str) -> Awaitable[str]: if len(name) > 8: raise ValueError(f"{name} is too long...") for ii in range(len(name)): print(name, ii) await asyncio.sleep(1) return name Now, running this task twice: await count_word("first"), await count_word("second") first 0 first 1 first 2 first 3 first 4 second 0 second 1 second 2 second 3 second 4 second 5 ('first', 'second') Will not run the two tasks concurrently because it’s using await, which means that it will wait for the first task to complete before starting the second task. If you want to run these tasks concurrently, you should use asyncio.gather(): ...

October 23, 2023 · 3 min · 530 words · Joost

Summarize large files - an introduction

ChatPDF providers, where you can question large files with large language models (LLM’s), are sprouting like mushrooms. The technique is mainly based on vector embedding with a vector index or vector database; based on the question, semantically relevant chunks from the file are provided to the LLM so it can compose an answer. While this technique is cool, it is limited when you ask a question that spans the entire text, such as generating a summary, since that requires not a couple of chunks, but the full text. ...

July 31, 2023 · 4 min · 716 words · Joost

Interactive Scala with Almond

Almond is a Scala kernel for Jupyter. Some features: Ammonite, a Scala REPL implementation. Coursier, an artefact manager. You can deploy Almond on Kubernetes with the following manifest: apiVersion: apps/v1 kind: Deployment metadata: name: almond labels: app: almond spec: replicas: 1 selector: matchLabels: app: almond template: metadata: labels: app: almond spec: containers: - name: almond image: almondsh/almond:0.13.11 resources: requests: memory: 384Mi limits: memory: 384Mi ports: - containerPort: 8888 --- kind: Service apiVersion: v1 metadata: name: almond spec: type: ClusterIP selector: app: almond ports: - protocol: TCP port: 8888 targetPort: 8888 --- kind: Service apiVersion: v1 metadata: name: almond-headless spec: clusterIP: None selector: app: almond Port forward: ...

May 7, 2023 · 1 min · 193 words · Joost

Some hatch mechanics 📐🔨

I am often a DIY’er and I have a really heavy hatch (32 kg) at home that I want to open automatically with an electric actuator. Something like the hatch in the image below. You can buy these actuators for 50 to 100 euros online: but which one do I need? or: What is the minimal and maximal length (and stroke) that I need? What is the force the actuator should be able to push? I made a schematic model, with ...

January 20, 2023 · 2 min · 385 words · Joost

Collecting stickers 📷

My local supermarket runs a campaign where you can collect stickers for a photo album 📔 with historic pictures of our pitoresk town. For every 10 euros you get a package of 4 stickers and in total there are 229 unique stickers. My question is: How many stickers including doubles do you need to get a full album? 💁 This is known as the Coupon collector’s problem (a variant of the birthday problem). The first card you will always have a new unique card. The change that the second card is a new unique card is $1$ minus the change that you have the same card as the first: ...

January 1, 2023 · 2 min · 321 words · Joost