多进程 多线程 异步 爬虫(2)

异步爬虫实战

接上节多进程 多线程 异步 爬虫(1)

asyncio文档
asyncio具体特性

Lock(锁)

class AsyncAiohttp_Lock(AsyncAiohttp):
    def __init__(self):
        super(AsyncAiohttp_Lock,self).__init__()
        self._time_lock = LOCK_TIME

    def unlock(self,lock):
        lock.release()

    async def url_dict_onepage_async(self,*agrs):
        with await agrs[1]:
            await super(AsyncAiohttp_Lock,self).url_dict_onepage_async( agrs[0])

    async def tasks_lock(self,loop,urls):
        #创建一个锁
        lock = asyncio.Lock()
        #加锁
        await lock.acquire()
        #loop.call_later(delay, func, *args)延迟delay秒之后再执行。
        #loop.call_at(when, func, *args) 某个时刻才执行。
        #loop.call_soon( func, *args)顺序执行

        #延迟self._time_lock 时间后执行 解锁
        #事件循环变慢
        loop.call_later(self._time_lock,functools.partial(self.unlock,lock))
        await asyncio.wait([self.url_dict_onepage_async(url,lock) for url in urls])

    def page_ELoop(self, urls):
        # 获取EventLoop
        loop = asyncio.get_event_loop()
        # tasks = [self.url_dict_onepage_async(url) for url in urls]
        # 执行coroutine
        loop.run_until_complete(self.tasks_lock(loop,urls))
        loop.close()

Event(事件)

'''
Event(事件)
Class implementing event objects. An event manages a flag that can be set<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值