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 pants made from waterproof or water-resistant materials, such as nylon, polyester, or technical fabrics with a DWR (Durable Water Repellent) coating. Avoid cotton, as it absorbs water and becomes heavy and cold. Trousers with sealed seams and adjustable cuffs are ideal for keeping rain out.
Shirt: You should wear a long-sleeved shirt made of tightly woven fabric or fabric specifically designed to block UV rays (UPF rated). Lighter colors are generally better as they reflect sunlight, but dark colors can also offer good protection if the fabric is dense. A hat and sunglasses are also recommended for additional protection.
Shoes: You should wear waterproof boots with deep treads, such as rubber wellington boots or hiking boots, to provide traction and keep your feet dry.
Finished in 1.958 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: You should wear waterproof or water-resistant pants, such as rain pants, shell pants, or trousers made from synthetic materials like nylon or polyester. Avoid cotton, as it absorbs water and stays wet.
Shirt: You should wear a long-sleeved shirt made of tightly woven fabric or specifically designed UPF (Ultraviolet Protection Factor) clothing to protect your skin from the sun. Light colors are also recommended as they reflect sunlight better than dark colors.
Shoes: You should wear waterproof boots with deep treads, such as rubber wellington boots (Wellies) or waterproof hiking boots, to provide traction and keep your feet dry.
Finished in 0.673 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)
One of the most mysterious and terrifying creatures in human history is the **Loch Ness Monster**. Often referred to as "Nessie," this legendary aquatic entity is said to inhabit Loch Ness in the Scottish Highlands. Descriptions typically depict a large, serpentine creature with a long neck and humps rising above the water's surface. The legend gained widespread popularity in the 1930s following a photograph that was later revealed to be a hoax, but the allure of the myth persists. While scientific expeditions have used sonar and underwater drones to search for evidence, no definitive proof of its existence has ever been found. The fear associated with Nessie stems from the unknown depths of the loch and the primal human fear of large, unseen predators lurking beneath the surface.