service-streamer icon indicating copy to clipboard operation
service-streamer copied to clipboard

如何配合asyncio进行工作

Open JimLee1996 opened this issue 4 years ago • 4 comments

作者你好,项目非常实用,也写的非常好。

最近我在配合aiokafka+asyncio+service_streamer的时候遇到了问题,即调用streamer的时候发生了阻塞,streamer的输出日志中,导致每个task的batch为1。

我的疑惑如下:

  1. 问题的原因是不是因为streamer的实现是针对多线程(比如flask)或者多进程的,对于原生的python协程而言,会阻塞掉当前的loop。

  2. 有没有什么比较好的方法或者小trick可以解决这个问题

希望你帮忙看看并提些建议吧,谢谢~

        async for msg in consumer:
            recv_time = int(time.time() * 1000)
            passId = msg.value['passId']
            imgURL = msg.value['imgURL']
            imgPath = msg.value['path']

            # get image
            img = await fetcher.get_img(imgURL)

            # TODO async
            # backend process
            item = streamer.predict([img])[0]

            # post result
            send_time = int(time.time() * 1000)
            result = create_result(passId, recv_time, send_time, imgPath, item)
            await poster.post_result(result)
image

JimLee1996 avatar Dec 14 '19 11:12 JimLee1996

用asyncio的办法重构了一个吧,我在做一样事情,里面的很多component不能直接放去asyncio里面

AgingChan avatar Mar 05 '20 09:03 AgingChan

遇到相同的需求,请问两位有解决的方案吗? @AgingChan @JimLee1996

MAhaitao999 avatar Jan 21 '21 08:01 MAhaitao999

@MAhaitao999 我自己写了一个

AgingChan avatar Jan 22 '21 23:01 AgingChan

@MAhaitao999 我自己写了一个

欢迎提PR到这个仓库, 我可以帮忙review

Meteorix avatar Jan 26 '21 05:01 Meteorix