Python - 关于“多进程”并发使用的问题

发布于 2024-08-30 09:27:58 字数 242 浏览 4 评论 0原文

我想使用Python的多处理来进行并发处理而不使用锁(锁对我来说与多处理相反),因为我想在Web请求期间同时从不同的资源构建多个报告(通常需要大约3秒,但是通过多重处理,我可以在 0.5 秒内完成)。

我的问题是,如果我向网络公开这样的功能并让 10 个用户同时提取相同的报告,我突然会同时打开 60 个解释器(这会使系统崩溃 )。这只是使用多处理的常识结果,还是有一个技巧可以解决这个潜在的噩梦?

谢谢

I want to use Python's multiprocessing to do concurrent processing without using locks (locks to me are the opposite of multiprocessing) because I want to build up multiple reports from different resources at the exact same time during a web request (normally takes about 3 seconds but with multiprocessing I can do it in .5 seconds).

My problem is that, if I expose such a feature to the web and get 10 users pulling the same report at the same time, I suddenly have 60 interpreters open at the same time (which would crash the system). Is this just the common sense result of using multiprocessing, or is there a trick to get around this potential nightmare?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被翻牌 2024-09-06 09:27:58

如果您确实担心实例过多,您可以考虑使用信号量对象来保护调用。如果我理解您在做什么,那么您可以使用线程信号量对象:

from threading import Semaphore
sem = Semaphore(10)
with sem:
    make_multiprocessing_call()

我假设 make_multiprocessing_call() 将自行清理。

这样,只会打开 10 个“额外”的 python 实例,如果出现另一个请求,则只需等待前一个请求完成即可。不幸的是,这不会按照“队列”顺序......或任何特定的顺序。

希望有帮助

If you're really worried about having too many instances you could think about protecting the call with a Semaphore object. If I understand what you're doing then you can use the threaded semaphore object:

from threading import Semaphore
sem = Semaphore(10)
with sem:
    make_multiprocessing_call()

I'm assuming that make_multiprocessing_call() will cleanup after itself.

This way only 10 "extra" instances of python will ever be opened, if another request comes along it will just have to wait until the previous have completed. Unfortunately this won't be in "Queue" order ... or any order in particular.

Hope that helps

夕嗳→ 2024-09-06 09:27:58

如果您尝试使用多进程向网络应用程序添加并发性,那么您就找错了方向。如果您为每个请求创建流程,那么您就是在树上一棵完全错误的树。 multiprocess 不是您想要的(至少作为并发模型)。

您很有可能需要一个异步网络框架,例如 Twisted

You are barking up the wrong tree if you are trying to use multiprocess to add concurrency to a network app. You are barking up a completely wrong tree if you're creating processes for each request. multiprocess is not what you want (at least as a concurrency model).

There's a good chance you want an asynchronous networking framework like Twisted.

寂寞清仓 2024-09-06 09:27:58

仅当您有多个代理写入源时,才需要锁。如果他们只是访问,则不需要锁(正如您所说,违背了多处理的目的)。

你确定这会让系统崩溃吗?在使用 CGI 的 Web 服务器上,每个请求都会生成一个新进程,因此看到数千个并发进程并不罕见(在 python 中,应该使用 wsgi 并避免这种情况),这不会导致系统崩溃。

我建议你测试一下你的理论——制造 10 个同时访问应该不难——然后看看你的服务器是否真的崩溃了。

locks are only ever nessecary if you have multiple agents writing to a source. If they are just accessing, locks are not needed (and as you said defeat the purpose of multiprocessing).

Are you sure that would crash the system? On a web server using CGI, each request spawns a new process, so it's not unusual to see thousands of simultaneous processes (granted in python one should use wsgi and avoid this), which do not crash the system.

I suggest you test your theory -- it shouldn't be difficult to manufacture 10 simultaneous accesses -- and see if your server really does crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文