My script works as expected except that it ignores the if condition inside async def get_item - no basic errors I can see, "==" is used for if, type for comparison are both string and proof (?) that the if test is being ignored is that there is no output from the line, print("INSIDE IF"). No error messages are output.
import asyncio
AList = [['A','R','M'],
['B','W','N'],
['C','W','O'],
['D','R','P'],
]
num_items = len(AList) """ is 4 in this example - a list of lists"""
Asub = [] """ a yet to be nominated sub list of AList"""
List_out= [] """ output list - empty at start."""
N = 0
""" define a coroutine to simulate retrieval of an item"""
async def get_item(i):
global N
N = N + 1 """ used to count passes"""
Asub = AList[i] """nominated sub list (i range 0 to 3 for example)"""
value1 = Asub[1:2] """ value of interest - second item """
value1_str = str(value1) """ being specific about type"""
ref_val = 'W' """being specific about type"""
print("TYPE", type(value1_str), value1_str)
if value1_str == ref_val: """ should be true in 2 cases for example"""
print("INSIDE IF") """debug check"""
value1_str = 'Water'
else:
value1_str = value1_str
await asyncio.sleep(0.1)
print("PassCount", N, "ValueOut", value1_str)
List_out.append(value1_str)
OutLen = len(List_out)
print( "InListLen: ", num_items,"OutListLen ", OutLen)
return f'item {List_out}'
""" define a coroutine which executes several coroutines """
async def get_items(num_items):
print('getting items')
""" create a list of Tasks """
item_coros = [asyncio.create_task(get_item(i)) for i in range(num_items)]
print('waiting for tasks to complete')
""" wait for all Tasks to complete """
completed, pending = await asyncio.wait(item_coros)
"""access the Task results """
print(List_out)
"""create an event loop """
loop = asyncio.get_event_loop()
try:
"""execute the coroutine in the event loop """
loop.run_until_complete(get_items(num_items)) #num_items also works
finally:
loop.close()
Output from the above is more or less as expected - but without any evidence of the if condition being processed.
Outputs - with no change to 'W'
There seems to be little on the issue on line, and I have seen the same error in the work of a "professional" !!
This one has beaten me - any assistance much appreciated.