我正在使用aiohttp来创建一个小的webapp。我有这个方法:
import logging as log
class Streamer(object):
[...]
def _update(self, id): # @ReservedAssignment
loop = self.loop
analysis_streamer = self.analysis_streamer
analysis_streamer.runAnalyses(id, loop=loop)
print("Analyses done")
和AnalysisStreamer
有以下方法:
class AnalysisStreamer(object):
[...]
def runAnalyses(self, id, loop=None):
analyses = self.queue.get(id, ())
for analysis in analyses:
self.runAnalysis(analysis, loop=loop)
def runAnalysis(self, analysis, loop=None):
log.debug("Executing {a}".format(a=analysis))
analysis.run(loop=loop)
和Analysis
有这种方法:
class Analysis(object):
[...]
def run(self, loop=None):
"""
Run the analysis in a separate thread from asyncio loop
@author Marco Sulla (marcosullaroma@gmail.com)
@date Apr 28, 2016
@param loop: asyncio loop to use to launch the thread. If none is
provided, the default loop is used
"""
@asyncio.coroutine
def runInThread(self):
# code here
if loop is not None:
loop.call_soon_threadsafe(
asyncio.async,
runInThread(self)
)
else:
asyncio.async(runInThread(self))
它没有问题。我尝试在协程中转换runAnalyses()
方法,只需执行
yield from analysis_streamer.runAnalyses(id, loop=loop)
但这样代码就不再起作用了。 <{1}}将在此时暂停,_update()
永远不会打印。
PS:我正在使用python 3.4.3