Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Concurrent.Futures错误[TypeError:';非类型';对象不可调用]_Python_Typeerror_Python Asyncio_Nonetype - Fatal编程技术网

Python:Concurrent.Futures错误[TypeError:';非类型';对象不可调用]

Python:Concurrent.Futures错误[TypeError:';非类型';对象不可调用],python,typeerror,python-asyncio,nonetype,Python,Typeerror,Python Asyncio,Nonetype,所以我设法让asyncio/谷歌CSE API一起工作。。。。当我在PyCharm上运行代码时,我能够打印出结果。然而,在打印内容的最后是错误“TypeError:'NoneType'对象不可调用” 我怀疑这与我的列表有关,也许循环试图搜索另一个术语,即使我在列表的末尾 还有。。这是我的第一个问题帖子,所以请随时提供关于如何更好地提问的建议 想法 searchterms = ['cheese', 'hippos', 'whales', 'beluga'] async d

所以我设法让asyncio/谷歌CSE API一起工作。。。。当我在PyCharm上运行代码时,我能够打印出结果。然而,在打印内容的最后是错误“TypeError:'NoneType'对象不可调用”

我怀疑这与我的列表有关,也许循环试图搜索另一个术语,即使我在列表的末尾

还有。。这是我的第一个问题帖子,所以请随时提供关于如何更好地提问的建议

想法

searchterms = ['cheese',
    'hippos',
    'whales',
    'beluga']

async def sendQueries(queries, deposit=list()):
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        loop = asyncio.get_event_loop()
        futures = [
            loop.run_in_executor(
                executor,
                searching(queries)
            )
        ]
        for response in await asyncio.gather(*futures):
            deposit.append(response.json())
        return deposit

def running():
     loop = asyncio.get_event_loop()
     loop.run_until_complete(loop.create_task(sendQueries(searchterms)))
     loop.close()

print(running())
print(str(time.time() - x))
我的错误可以追溯到“for response in wait asyncio.gather(*futures):”


仅供参考,搜索(查询)只是我的Google CSE API调用的函数

问题在于调用
run\u in\u executor

    futures = [
        loop.run_in_executor(
            executor,
            searching(queries)
        )
    ]
在执行器中运行
接受要执行的函数。代码没有向它传递函数,它调用函数,
搜索,并传递该调用的返回值。这有两个后果:

  • 代码没有按预期工作-它逐个调用搜索,而不是并行调用

  • 它显示一个错误,表示
    run\u in\u executor
    试图调用
    搜索(…)
    None
    返回值。令人困惑的是,只有在等待由执行器中的运行返回的期货时,才会出现错误,此时所有搜索实际上已经完成

  • 调用
    run\u in\u executor
    的正确方法如下:

        futures = [
            loop.run_in_executor(executor, searching, queries)
        ]
    
    请注意,
    搜索
    功能现在仅被提及而未被使用


    此外,如果您只使用asyncio调用
    run\u in\u executor
    中的同步调用,那么您并没有真正从它的使用中获益。您可以直接从
    concurrent.futures
    使用基于线程的工具获得相同的效果,但无需将整个程序调整为asyncio
    run\u in\u executor
    旨在节省使用,用于偶尔与不提供异步前端的旧式API进行接口,或用于无法有意义地转换为协同程序的CPU密集型代码。

    无关:您应该仔细阅读。您可能会遇到存款问题。好的。谢谢你的帮助。它现在肯定工作得更快了。我在1.1秒内得到了大约12个搜索实例,而不是之前的5个。我以为那时候有些事情很粗略。。。。。