Login light
import asyncio
import random
import time

class spider:
    output = []
    def __init__(self):
        self.gen = self.pageUrl()
        loop = asyncio.get_event_loop()
        tasks = [self.main(i) for i in range(2)]
        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()
    
    def pageUrl(self):
        for i in range(10):
            yield i

    def saveData(self, data, flag = False):
        if flag and len(self.output) > 0:
            print('saveData:', self.output)
            self.output = []
            return
        self.output.append(data)
        if len(self.output) == 3:
            print('saveData:', self.output)
            self.output = []

    async def main(self, number):
        while True:
            try:
                url = self.gen.send(None)
            except StopIteration:
                break
            print('number:',number,'do something:',url)
            # 异步请求
            ret = await asyncio.sleep(1)
            # 异步请求2
            print('number:',number,'do something2:',url)
            ret = await asyncio.sleep(1)

            self.saveData((url, number))

if __name__ == '__main__':
    spider()