简体   繁体   English

扭曲的 python 挂在大型多部分消息上

[英]twisted python hangs on large multipart messages

At my company we are using twisted and flask to run a rest api service on an industrial computer.在我公司,我们使用 twisted 和 flask 在工业计算机上运行 rest api 服务。 This API is used partially for IPC and for limited communication from external sources.此 API 部分用于 IPC 和来自外部资源的有限通信。 We've recently found a problem where twisted hangs when sending files via multipart post.我们最近发现了通过多部分帖子发送文件时出现扭曲挂起的问题。 Files around 600MB will hang twisted for about 40-50 seconds, no requests are processed during this time.大约 600MB 的文件将扭曲挂起大约 40-50 秒,在此期间不处理任何请求。

I should say I am not a web developer, I'm learning as I go.我应该说我不是 web 开发人员,我正在学习 go。 I've analyzed the issue and isolated it to the assembly of the multipart message, flask hangs on this as well but doesn't block messages.我已经分析了这个问题并将其隔离到多部分消息的组合中,flask 也挂在这个问题上,但不会阻止消息。 There have been a couple of bug reports for the same/similar issue: https://twistedmatrix.com/trac/ticket/5511 .对于相同/类似的问题,有几个错误报告: https://twistedmatrix.com/trac/ticket/5511 Problem is these are from 8 years ago, and I'm not concerned with streaming, or at least I don't think I am.问题是这些是 8 年前的,我不关心流媒体,或者至少我认为我不关心。

Are just out of luck?只是运气不好? Is there anything we can do to twisted for this work, I thought perhaps a configuration issue.我们可以为这项工作做些什么来扭曲,我想也许是配置问题。 Or perhaps an alternative to twisted, oddly enough the WSGI that comes with flask doesn't have this problem, but it is not for production use so it's not an option.或者也许是扭曲的替代方案,奇怪的是,flask 附带的 WSGI 没有这个问题,但它不适合生产使用,所以它不是一个选项。

Apart from implementing a fix from the problem, you may indeed be out of luck.除了实施解决问题的方法外,您可能确实不走运。 The reason you notice this problem with Twisted is that your Twisted Web server is almost certainly entirely single threaded (this is the default and doing otherwise requires some non-trivial effort).您注意到 Twisted 这个问题的原因是您的 Twisted Web 服务器几乎肯定是完全单线程的(这是默认设置,否则需要一些不小的努力)。 When the single Twisted reactor thread starts parsing the huge upload, nothing else can be served until it has finished parsing that huge upload.当单个 Twisted reactor 线程开始解析巨大的上传时,在完成对巨大上传的解析之前,无法提供任何其他服务。 And as you've discovered, the parser is quite slow.正如您所发现的,解析器非常慢。

You don't observe this problem (at least not under the same conditions) with a WSGI-based server because those servers are running multiple threads or processes.您不会在基于 WSGI 的服务器上观察到这个问题(至少在相同的条件下),因为这些服务器正在运行多个线程或进程。 When one client uploads a large file, one of those threads or processes is probably blocked for a while but the others can continue to service other requests.当一个客户端上传一个大文件时,其中一个线程或进程可能会被阻塞一段时间,但其他客户端可以继续为其他请求提供服务。 If you had one client per thread or process and those clients all uploaded a large file, you would still block all of the threads/processes and other clients would not be serviced until those threads/processes finished that work.如果每个线程或进程有一个客户端,并且这些客户端都上传了一个大文件,那么您仍然会阻止所有线程/进程,并且在这些线程/进程完成该工作之前不会为其他客户端提供服务。

As I said at the top, this is not impossible to fix.正如我在顶部所说,这并非不可能解决。 It can be fixed in Twisted so that form parsing does not block the reactor thread until it has finished completely.它可以在 Twisted 中修复,以便表单解析在完全完成之前不会阻塞反应器线程。 There are likely other solutions as well.可能还有其他解决方案。 For example, you may be able to run multiple Twisted Web processes (perhaps sharing a single listening socket to make this transparent to clients) so that it at least behaves as well as the WSGI server you observed.例如,您可能能够运行多个 Twisted Web 进程(可能共享一个侦听套接字以使其对客户端透明),以便它的行为至少与您观察到的 WSGI 服务器一样好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM