Issue
As a newbie in python AsyncIO, I have written a sample cook and waiter problem.
import asyncio
async def waiter():
t1 = asyncio.create_task(cook('indian', 10))
t2 = asyncio.create_task(cook('chinese', 5))
t3 = asyncio.create_task(cook('american', 15))
await t1
await t2
await t3
async def cook(name, time):
print('Preparing {}'.format(name))
await asyncio.sleep(time)
print('Prepared {}'.format(name))
asyncio.run(waiter())
ubuntu@ip-172-31-14-144:~$ python3 one.py
Preparing indian
Preparing chinese
Preparing american
Prepared chinese
Prepared indian
Prepared american
ubuntu@ip-172-31-14-144:~$
I understand from the above one.py that, the waiter takes all the orders and then gives to the cook to process them. So to further build up my understanding I thought of making a menu driven program, so that the user can choose.
import asyncio
import aioconsole
menu = {
'item1': 10,
'item2': 5,
'item3': 25,
'item4': 5
}
queue = asyncio.Queue()
tasks = []
async def cook():
print('In queue')
user_option = await queue.get()
user_option -= 1
print(user_option)
print('Preparing {}'.format(list(menu.keys())[user_option-1]))
await asyncio.sleep(menu[list(menu.keys())[user_option-1]])
print('Prepared {}'.format(list(menu.keys())[user_option-1]))
async def get_input():
inp = await aioconsole.ainput('Please enter your desired option\n')
return int(inp)
async def waiter():
user_option = 0
while True:
count = 1
print('*'*100)
print('Hello User..\n')
print('What would you like to have ??\n')
for item in menu:
print('{}. {}'.format(count, item))
count = count + 1
try:
user_option = await asyncio.wait_for(get_input(), timeout=2.0)
except asyncio.TimeoutError:
print('TIMEOUT')
if user_option:
await queue.put(user_option)
tasks.append(asyncio.create_task(coro=cook()))
for i in tasks:
await i
else:
print('In else')
pass
asyncio.run(waiter())
****************************************************************************************************
Hello User..
What would you like to have ??
1. item1
2. item2
3. item3
4. item4
Please enter your desired option
TIMEOUT
In else
****************************************************************************************************
Hello User..
What would you like to have ??
1. item1
2. item2
3. item3
4. item4
Please enter your desired option
1 -> an option is entered here
In queue
0
Preparing item4 # Item is being prepared, but the intention is this should be happening
Prepared item4 # concurrently, so that other users can place their order
****************************************************************************************************
Hello User..
What would you like to have ??
1. item1
2. item2
3. item3
4. item4
Please enter your desired option
Expectation: In the second program when an option is entered, the cook should process them and print them on the screen concurrently, while an user can place their order, even when cook is preparing some thing.
Problem: As soon as an option is entered, the waiter function waits for the cook to complete and then displays the menu.
Python 3.8.10 is used
Thanks
Solution
import asyncio
import aioconsole
menu = {
'item1': 10,
'item2': 5,
'item3': 25,
'item4': 5
}
tasks = []
#async def cook(queue):
#
# print('In queue')
# user_option = await queue.get()
# user_option -= 1
# print(user_option)
# print('Preparing {}'.format(list(menu.keys())[user_option-1]))
# await asyncio.sleep(menu[list(menu.keys())[user_option-1]])
# print('Prepared {}'.format(list(menu.keys())[user_option-1]))
#
async def cook(queue):
while True:
print('In queue')
user_option = await queue.get()
user_option -= 1
print(user_option)
print('Preparing {}'.format(list(menu.keys())[user_option-1]))
await asyncio.sleep(menu[list(menu.keys())[user_option-1]])
print('Prepared {}'.format(list(menu.keys())[user_option-1]))
async def get_input():
inp = await aioconsole.ainput('Please enter your desired option\n')
return int(inp)
async def waiter(queue):
user_option = 0
while True:
count = 1
print('*'*100)
print('Hello User..\n')
print('What would you like to have ??\n')
for item in menu:
print('{}. {}'.format(count, item))
count = count + 1
try:
user_option = await asyncio.wait_for(get_input(), timeout=1.0)
print('You entered {}'.format(user_option))
except asyncio.TimeoutError:
pass
if user_option > 0:
print('Inserting option into queue {}'.format(user_option))
await queue.put(user_option)
user_option = -1
await asyncio.sleep(3)
async def main():
queue = asyncio.Queue()
task1 = asyncio.create_task(waiter(queue))
task2 = asyncio.create_task(cook(queue))
await asyncio.gather(task1, task2)
asyncio.run(main())
The waiter can now concurrently take orders and the cook function prints when the item is prepared.
Answered By - Shubham Ranjan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.