Python异步代理状态检查

时间:2017-09-05 22:36:21

标签: python asynchronous

美好的一天,我试图在python中掌握异步的东西,并陷入了一个应该很容易解决的情况,但我仍然感到沮丧我做错了什么。建议我有一个代理列表,我想检查服务器是否已启动并运行,更重要的是我想以异步方式执行此操作,遗憾的是,与同步功能的比较显示了执行的相同时间。

所以,代码:

import requests
import os
import asyncio

from lxml import html


class GetUkProxy:
    url = "https://free-proxy-list.net/uk-proxy.html"

    def __init__(self):
        self.getProxyList(self.url)

    def getProxyList(self, url):
        self.proxies = []
        data = requests.get(url)
        response = html.fromstring(data.text)
        for item in response.xpath('//table[@id="proxylisttable"]/tbody/tr'):
            ip, port = item.getchildren()[:2]
            self.proxies.append({'IP': ip.text, 'Port': port.text, 'Status': 'unknown'})

    def printProxies(self):
        try:
            for proxy in self.proxies:
                print(proxy['IP'] + ':' + proxy['Port'], proxy['Status'])
        except:
            print("Proxy list is empty")

    def checkProxies(self):
        loop = asyncio.get_event_loop()
        data = loop.run_until_complete(self.checkProxyList())
        loop.close()


    async def checkProxyList(self):
        try:
            tasks = []
            for proxy in self.proxies[:10]:
                task = asyncio.ensure_future(self.checkProxy(proxy))
                tasks.append(task)
            await asyncio.gather(*tasks)
        except:
            print('Proxy list is empty')

    async def checkProxy(self, proxy=None):
        try:
            check = os.system('ping -n 4 -w 2 ' + proxy['IP'])
            if not check:
                proxy['Status'] = 'Dead'
            else:
                proxy['Status'] = 'Alive'
        except Exception as exp:
            print(exp)

    def syncCheck(self):
        for item in self.proxies[:10]:
            check = os.system('ping -n 4 -w 2 ' + item['IP'])

异步checkProxies的执行时间等于执行同步syncCheck的时间,因此任何想法是错误的根源以及如何解决它是受欢迎的。

1 个答案:

答案 0 :(得分:1)

$daemon->run( name => 'server', host => 'localhost', port => 7111, min_servers => 1, max_servers => 1, min_spare_servers => 0, max_spare_servers => 0, background => 0, # <------ here setsid => 0, # <------ and here ("Process will also background if setsid is set") ); 是一个阻止事件循环的同步函数。

您可以使用os.system异步执行系统命令,并阻止阻止事件循环。

asyncio.create_subprocess_exec

修改:在async def checkProxy(self, proxy=None): try: check = await asyncio.create_subprocess_exec( 'ping', '-c', '4', '-W', '2', proxy['IP'], stdout=asyncio.subprocess.PIPE) out, error = await check.communicate() proxy['Status'] = 'Alive' if error: proxy['Status'] = 'Dead' except Exception as exp: print(exp) 方法中,我们会制作一起运行的期货清单。

checkProxies