Python 子线程与主线程的异步性能差异

Python 子线程与主线程的异步性能差异,python,multithreading,python-asyncio,python-multithreading,Python,Multithreading,Python Asyncio,Python Multithreading,我的环境 操作系统:Windows10 Python 3.6.7 库:Python elasticsearch 7.12.0 我尝试在子线程中执行异步IO事件循环。 执行速度非常慢(约14秒) 但是,当我在主线程中执行此命令时,执行速度非常快(大约0.8秒) 我不知道这些代码有什么区别。 在子线程和主线程中执行异步IO有什么区别吗 子线程版本 主线程版本 import asyncio import timeit import threading from elasticsearch impo

我的环境

  • 操作系统:Windows10
  • Python 3.6.7
  • 库:Python elasticsearch 7.12.0
我尝试在子线程中执行异步IO事件循环。 执行速度非常慢(约14秒) 但是,当我在主线程中执行此命令时,执行速度非常快(大约0.8秒)

我不知道这些代码有什么区别。 在子线程和主线程中执行异步IO有什么区别吗

  • 子线程版本
  • 主线程版本
  • import asyncio
    import timeit
    import threading
    from elasticsearch import AsyncElasticsearch
    
    class Elastic:
        def __init__(self, host):
            self.es = AsyncElasticsearch([host], timeout=60)
    
        async def async_fetch(self, query):
            try:
                resp = await self.es.search(
                    index=query['index'],
                    size=query['size'],
                    sort=query['sort']
                )
                return resp
            except Exception as e:
                print("ERROR!!!", e)
    
    class Getter:
        def __init__(self):
            print('init!')
    
        def get_query_list(self):
            query_list = []
            for i in range(1, 100):
                query_list.append({
                    'index': 'test-index',
                    'size': 10,
                    'sort': ['@timestamp:desc']
                })
            return query_list
    
        async def async_fetch_all(self, query_list):
            async def fetch_query(query):
                model = Elastic("http://root:test@127.0.0.1:9200")
                res = await model.async_fetch(query)
    
            await asyncio.gather(*[fetch_query(query) for query in query_list], return_exceptions=True)
    
        def get_data(self):
            query_list = self.get_query_list()
            self.loop.run_until_complete(self.async_fetch_all(query_list))
    
        def get_data(self):
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
    
            loop.run_until_complete(self.async_fetch_all())
            loop.close()
    
    class GetThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self, name="slow thread")
            print('slow thread')
    
        def run(self):
            getter = Getter()
            getter.get_data()
    
    def run_service():
        th = GetThread()
        th.daemon = True
        th.start()
    
        while True:
            continue
    
    if __name__ == '__main__':
        run_service()
    
    import asyncio
    import timeit
    import threading
    from elasticsearch import AsyncElasticsearch
    
    class Elastic:
        def __init__(self, host):
            self.es = AsyncElasticsearch([host], timeout=60)
    
        async def async_fetch(self, query):
            try:
                resp = await self.es.search(
                    index=query['index'],
                    size=query['size'],
                    sort=query['sort']
                )
                return resp
            except Exception as e:
                print("ERROR!!!", e)
    
    class Getter:
        def __init__(self):
            print('init!')
    
        def get_query_list(self):
            query_list = []
            for i in range(1, 100):
                query_list.append({
                    'index': 'test-index',
                    'size': 10,
                    'sort': ['@timestamp:desc']
                })
            return query_list
    
        async def async_fetch_all(self, query_list):
            async def fetch_query(query):
                model = Elastic("http://root:test@127.0.0.1:9200")
                res = await model.async_fetch(query)
    
            await asyncio.gather(*[fetch_query(query) for query in query_list], return_exceptions=True)
    
        def get_data(self):
            query_list = self.get_query_list()
            self.loop.run_until_complete(self.async_fetch_all(query_list))
    
        def get_data(self):
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
    
            loop.run_until_complete(self.async_fetch_all())
            loop.close()
    
    def run_service():
        getter = Getter()
        getter.get_data()
    
        while True:
            continue
    
    if __name__ == '__main__':
        run_service()