Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python yield async await thread function

import asyncio

async def createGenerator():
    mylist = range(3)
    for i in mylist:
        await asyncio.sleep(1)
        yield i*i

async def start():
    async for i in createGenerator():
        print(i)

loop = asyncio.get_event_loop()

try:
    loop.run_until_complete(start())

except KeyboardInterrupt:
    loop.stop()
    pass
Comment

python yield async await thread function

import asyncio


async def async_generator():
    for i in range(3):
        await asyncio.sleep(1)
        yield i*i


async def main():
    async for i in async_generator():
        print(i)


loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
finally:
    loop.run_until_complete(loop.shutdown_asyncgens())  # see: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.shutdown_asyncgens
    loop.close()
Comment

PREVIOUS NEXT
Code Example
Python :: python async get result 
Python :: is c++ easier than python 
Python :: python Pandas - Analyzing DataFrames 
Python :: how to strip characters in python 
Python :: run c code in python 
Python :: python literation (looping) 
Python :: pandas merge keep one of column copy 
Python :: python enum key string get 
Python :: python how to not allow class instance 
Python :: Lightbank b2c 
Python :: enumerate count 
Python :: Python List insert() add element at designated place 
Python :: way to close file python with staement 
Python :: Python - Perl - Tcl 
Python :: eastcoders: django-meta-class 
Python :: jupyter notebook print formatted text 
Python :: python solve rubicks cube 
Python :: frontmost flag qt 
Python :: 56.5 to 57 in python 
Python :: a guide to numpy and pandas 
Python :: concat dataset 
Python :: python find multiple matches in string 
Python :: io.imsave 16 bit 
Python :: python print functoin 
Python :: Reading from a file way01 
Python :: py random sample 
Python :: WARNING: Ignoring invalid distribution -pencv-python 
Python :: scraped text in Russian encoding python 
Python :: python check if array alternating 
Python :: numpy topk 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =