- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个异步应用程序,它使用来自 aiohttp
的服务器和带有 asyncio.open_connection()
我的代码包含一些来自 PIL 库的阻塞调用,例如
Image.save()
Image.resize()
os.path.join()
是否被认为是正确的?如何处理 numpy
数组?最佳答案
can my web server freeze if I use these blocking calls? Moreprecisely, is it possible that the event loop will miss events becauseof blocking code?
服务器在执行图像函数时会精确卡住。您不会错过任何事件,但所有事件处理都将在图像函数执行时延迟。
卡住事件循环是一种糟糕的情况——你应该避免它。
If yes, what is the replacement for these functions, that integratewith asyncio? there is no asyncio version of PIL.
避免卡住事件循环的最简单和通用的方法 - 使用 asyncio.run_in_executor 在另一个线程或另一个进程中执行阻塞函数.那里的代码片段展示了如何做到这一点,并很好地解释了何时使用进程或线程:
def blocking_io():
# File operations (such as logging) can block the
# event loop: run them in a thread pool.
with open('/dev/urandom', 'rb') as f:
return f.read(100)
def cpu_bound():
# CPU-bound operations will block the event loop:
# in general it is preferable to run them in a
# process pool.
return sum(i * i for i in range(10 ** 7))
我只想补充一点,进程池可能并不总是适合每个 CPU 密集型操作的好解决方案。如果您的图像函数不会花费太多时间(或者特别是如果您的服务器没有多个处理器内核),那么在一个线程中运行它们可能仍然更有效率。
In general, what is considered a 'blocking code' in asyncio? besidesthe obvious operations like socket, read a file, etc. For example, doesos.path.join() is considered ok? what about working on a numpy array?
粗略地说任何函数都是阻塞的:它会阻塞事件循环一段时间。但是像 os.path.join
这样的许多函数只需要很少的时间,所以它们不是问题,我们不称它们为“阻塞”。
当执行时间(和事件循环卡住)成为问题时,很难说出确切的限制,特别是考虑到不同硬件的时间会有所不同。我有偏见的建议 - 如果您的代码在将控制权返回给事件循环之前需要(或可能需要)> 50 毫秒,请考虑它阻塞并使用 run_in_executor
。
更新:
Thanks, does it make sense to use one event loop (of the main thread),and using another thread that will add tasks using the same loop?
我不确定你在这里的意思,但我认为不是。我们需要另一个线程来运行一些作业,而不是在其中添加任务。
I need some way for the thread to inform the main thread after theimage processing is completed`
只需等待 run_in_executor
的结果或使用它启动任务。 run_in_executor
- 是一个协程,它在后台线程中执行某些操作而不阻塞事件循环。
它看起来像这样:
thread_pool = ThreadPoolExecutor()
def process_image(img):
# all stuff to process image here
img.save()
img.resize()
async def async_image_process(img):
await loop.run_in_executor(
thread_pool,
partial(process_image, img)
)
async def handler(request):
asyncio.create_task(
async_image_process(img)
)
# we use a task to return the response immediately,
# read https://stackoverflow.com/a/37345564/1113207
return web.Response(text="Image processed without blocking other requests")
关于python - PIL 和使用 asyncio 的阻塞调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53557304/
在 python 解释器中: >>> import PIL >>> PIL.Image Traceback (most recent call last): File "", line 1, in
我在 Pillow 中遇到过这个奇怪的错误,其中导入名称 PIL 需要全部小写而不是全部大写,但我从未见过 pil在任何地方都使用小写字母。这意味着我使用的导入 PIL 的所有 python 包和文件
有没有办法使用 PIL 截取驻留在我的服务器上的指定 HTML/Javascript 页面的屏幕截图? 我想编写一个脚本来更改该 HTML 页面上的一些参数,然后让 PIL 对其进行屏幕截图。 有什么
这是我所做的描述。 我正在尝试使用 PIL 编写程序,但是在尝试导入它时(如下所示),出现错误(也如下所示)。 from PIL import Image Here is the error. Tra
我正在尝试对图像进行简单的裁剪。这是代码 from PIL.Image import Image def get_image_half(image, half="upper"): if hal
我是一名新的Python用户,也是“Stack Overflow”中的新用户,当我尝试编译 tensorflow 代码时,我遇到了一些问题,并且我无法从网站上找到答案,所以我想从这里获得一些帮助,先谢
我知道 stackoverflow 上已经有很多与此问题相关的问题,我已经阅读了所有问题,但我仍然没有成功解决此问题。我希望有人能帮我解决这个问题。 我已经安装并重新安装了 Pillow 10 次。我
我得到错误: --------------------------------------------------------------------------- ImportError
我是机器学习的初学者,所以我试图创建一个模型来识别 Keras 博客中引用的图像。我已经在 Windows 10 上安装了 Anaconda 3 以及所有软件包,如tensorflow、keras、s
我正在尝试使用过滤器 FIND_EDGES 从图片中获取边缘,它在我的 Windows PC 上工作,但是当我在我的 Raspberry Pi 上运行相同的代码时,它给出了图像模式错误的错误。 最佳答
这个问题在 Python 环境中有一些答案,但这些解决方案不适用于我的 RStudio 环境。这是我的代码: library(keras) library(tensorflow) use_condae
我正在使用 Mac OS x 10.10.3 Yosemite 和 Python 2.7.9 |Anaconda 2.2.0 (x86_64) 来处理很多 python 的东西。我正在使用 Eclip
我正在遵循这个 SageMaker 指南并使用 1.12 cpu docker 文件。 https://github.com/aws/sagemaker-tensorflow-serving-cont
`from PIL import Image, ImageDraw, ImageFont image = Image.new('RGB', (950, 250), color=(255, 255, 2
我知道如何从图片中找到边缘。但我希望轮廓边缘更厚,例如宽度 9。 from PIL import Image, ImageFilter image = Image.open('your
我有多个白色背景的 PNG 图像,并且图像的某些部分充满了图案(可能是不同的颜色,黑色、蓝色、红色、黄色等)。 如何使用 Python PIL 库将所有这些图像合并为一张图像,以便所有非白色部分出
目前我正在尝试裁剪以下地址下文件夹内的所有图像:C:\\Users\\xie\\Desktop\\tiff\\Bmp然后将它们重新保存到同一个文件夹中。下面是我试图试验的代码,两者都运行没有错误但什么
虽然它们非常相似,但我确信 Pearson 相关相似度和调整余弦相似度之间存在一些差异,因为所有的论文和网页都将它们分为两种不同的类型。 然而,它们都没有提供明确的定义。 Here是其中一页。 谁能说
这是我的forms.py: class UploadImageForm(forms.ModelForm): class Meta: model = UserImages
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我是一名优秀的程序员,十分优秀!