我是asyncio的新手,我正在尝试使用loop.run_in_executor在单独的线程中运行阻止代码,但是似乎没有创建新的线程来运行阻止代码,并且它仍在主线程中运行。谁能帮助我了解如何以异步方式运行阻止操作?感谢所有帮助。
我尝试做async.sleep()以便将控件传递回主线程,但是由于已经调用了库,因此我不确定该怎么做。 loop.run_in_exeutor似乎由于某种原因无法正常工作。
async def get_xml(self, genome_key, query_string):
executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
loop = asyncio.get_event_loop()
log = logging.getLogger('run_blocking_tasks')
log.info('starting')
log.info('creating executor tasks')
blocking_tasks = loop.run_in_executor(executor, self.blocking_io, genome_key, query_string)
log.info('waiting for executor tasks')
completed = await asyncio.wait([blocking_tasks])
log.info('results: {!r}'.format(completed))
log.info('exiting')
这是阻止代码:
def blocking_io(self, genome_key, query_string):
return NCBIWWW.qblast("blastn","nt",query_string,megablast=False,expect="1000",word_size="7",nucl_reward="1",nucl_penalty="-3",gapcosts="5 2",entrez_query=self.genome_key_dict[genome_key])
函数get_xml的调用方式如下:
async def getMatchingLocations(self,query, loop):
r1 = self.get_max_alignment("nc_009899",query,loop)
r2 = await asyncio.gather(r1)
return r2
从烧瓶中调用函数getMatchingLocations像这样:
@application.route('/search',methods = ['POST'])
def getQueryResult():
q = request.form
myObj = Alignment_Check()
mLocations_s = loop.run_until_complete(myObj.getMatchingLocations(q.get("Name"), loop))
以下是示例输出:
> Thread-1 run_blocking_tasks: starting > Thread-1 run_blocking_tasks: creating executor tasks > Thread-1 run_blocking_tasks: waiting for executor tasks > Thread-1 asyncio: poll took 241953.000 ms: 1 events > Thread-1 run_blocking_tasks: results: ({> created at ***}, set()) Thread-1 run_blocking_tasks: exiting > As we can see that the thread too 241953.000 ms to complete and my application was doing nothing but sitting idle for that time. > The expected behavior is that the blocking function should be executed in a separate thread and once it is done executing I should be able to update my application with the results.