Skip to content

Futures

Easy Async

Futures are a powerful tool in Dandy that allow you to run code asynchronously which is great for non-blocking calls like network requests.

All you need to do on any class that has a process method is to call the process_to_future method instead.

Without Async Futures

Let's run three LlmBot.process calls at once and see how long it takes.

from time import perf_counter
from dandy import Bot

start_time = perf_counter()

pants_intel = Bot().process('What type of pants should I wear in the rain?')
shirt_intel = Bot().process('What type of shirt should I wear in the sun?')
shoes_intel = Bot().process('What type of shoes should I wear in the mud?')

print('Pants: ' + pants_intel.text)
print('Shirt: ' + shirt_intel.text)
print('Shoes: ' + shoes_intel.text)

print(f'Finished in {perf_counter() - start_time:.3f} seconds')
Pants: You should wear waterproof or water-resistant pants, such as rain pants or trousers made from materials like nylon or polyester with a durable water repellent (DWR) coating. Avoid cotton, as it absorbs water and becomes heavy and cold when wet.
Shirt: You should wear a shirt made of tightly woven fabric with a high Ultraviolet Protection Factor (UPF) rating. Long-sleeved shirts are generally better than short-sleeved ones for coverage. Lighter colors reflect more sunlight, but dark or bright colors often absorb UV rays better than white, though the weave density is the most important factor. Synthetic materials like polyester or nylon often offer better UV protection than cotton, which can become translucent when wet.
Shoes: You should wear waterproof boots with deep treads, such as rubber Wellington boots or rugged hiking boots, to provide traction and keep your feet dry in the mud.
Finished in 1.823 seconds

With Async Futures

Now let's run with futures, note you have to access the result attribute of the futures to get to the returned value.

Warning

We recommend you postfix your futures with _future to avoid naming conflicts and confusion.

from time import perf_counter
from dandy import Bot

start_time = perf_counter()

pants_intel_future = Bot().process_to_future('What type of pants should I wear in the rain?')
shirt_intel_future = Bot().process_to_future('What type of shirt should I wear in the sun?')
shoes_intel_future = Bot().process_to_future('What type of shoes should I wear in the mud?')

print('Pants: ' + pants_intel_future.result.text)
print('Shirt: ' + shirt_intel_future.result.text)
print('Shoes: ' + shoes_intel_future.result.text)

print(f'Finished in {perf_counter() - start_time:.3f} seconds')
Pants: For rainy weather, the best type of pants to wear are waterproof or water-resistant trousers. Key options include:

1. **Rain Pants / Trousers**: Specifically designed with sealed seams and waterproof materials (like Gore-Tex or nylon) to keep you completely dry.
2. **Hardshell Pants**: Lightweight, durable, and highly water-resistant, often used for outdoor activities.
3. **Water-Resistant Jeans**: Treated denim that repels water for short periods, though not ideal for heavy rain.
4. **Quick-Dry Synthetic Pants**: Made from materials like polyester or nylon that shed water and dry quickly if they get wet.

Avoid cotton jeans or chinos, as they absorb water, become heavy, and take a long time to dry, which can lead to discomfort or hypothermia in cold weather.
Shirt: You should wear a long-sleeved shirt made of tightly woven fabric or specifically designed UV-protection material. Light-colored shirts are also recommended as they reflect sunlight better than dark colors.
Shoes: You should wear waterproof boots with deep treads, such as rubber rain boots, hiking boots, or work boots, to provide traction and keep your feet dry.
Finished in 2.071 seconds

Advanced Async

Sometimes in more complex situations you might need to cancel a future or set a timeout.

from dandy import Bot

user_likes_scary_animals = True

cute_animal_future = Bot().process_to_future('Can you tell me about a random cute animal?')

scary_animal_future = Bot().process_to_future('Can you tell me about a random scary animal?')
scary_animal_future.set_timeout(seconds=30)

if user_likes_scary_animals:
    cute_animal_future.cancel()

print(scary_animal_future.result.text)
The Pufferfish is a random scary animal. While small, many species contain tetrodotoxin, a potent neurotoxin that can be fatal to humans if ingested, as it causes paralysis and respiratory failure.