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 in the rain to stay dry. Options include rain pants, waterproof trousers, or even pants made from water-repellent materials like nylon or polyester. Avoid cotton, as it absorbs water and stays wet.
Shirt: A light-colored, breathable shirt made of materials like cotton or moisture-wicking fabric is ideal for wearing in the sun. Look for shirts with UV protection (UPF rating) to help shield your skin from harmful UV rays.
Shoes: You should wear waterproof, sturdy boots with good traction, such as hiking boots or work boots, to keep your feet dry and prevent slipping in the mud.
Finished in 1.577 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 in the rain. Options include rain pants, waterproof trousers, or even pants made from materials like nylon or polyester with a water-repellent coating. Avoid cotton, as it absorbs water and stays wet.
Shirt: A light-colored, breathable shirt made of materials like cotton or moisture-wicking fabric is best for wearing in the sun. Look for one with UV protection (UPF rating) to help shield your skin from harmful ultraviolet rays.
Shoes: You should wear waterproof hiking boots or mud boots with good traction and a sturdy sole to keep your feet dry and stable in the mud.
Finished in 0.902 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 scariest animals is the box jellyfish. Found in the waters of the Indo-Pacific, it has tentacles that can grow up to 10 feet long and is armed with millions of tiny venom-filled stinging cells called nematocysts. A single sting can cause excruciating pain, cardiac arrest, and even death within minutes. Its transparent body makes it nearly invisible in the water, adding to its danger.