多处理还是多线程? [英] Multiprocessing or Multithreading?

查看:97
本文介绍了多处理还是多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,用于使用wxPython接口在Python中运行仿真.在程序中,您可以创建一个仿真,然后程序会为您渲染(=计算)它.渲染有时会非常耗时.

I'm making a program for running simulations in Python, with a wxPython interface. In the program, you can create a simulation, and the program renders (=calculates) it for you. Rendering can be very time-consuming sometimes.

当用户开始仿真并定义初始状态时,我希望程序在后台连续渲染仿真,而用户可能在程序中做不同的事情.有点像YouTube样式的条形图,该条形图充满了:您只能将模拟播放到渲染的点.

When the user starts a simulation, and defines an initial state, I want the program to render the simulation continuously in the background, while the user may be doing different things in the program. Sort of like a YouTube-style bar that fills up: You can play the simulation only up to the point that was rendered.

我应该使用多个进程或多个线程还是什么?人们告诉我使用multiprocessing程序包,我检查了它,它看起来不错,但是我还听说与线程不同,进程不能共享很多信息(而且我认为我的程序需要共享很多信息)信息.)此外,我还听说过Stackless Python:这是一个单独的选择吗?我不知道.

Should I use multiple processes or multiple threads or what? People told me to use the multiprocessing package, I checked it out and it looks good, but I also heard that processes, unlike threads, can't share a lot of information (and I think my program will need to share a lot of information.) Additionally I also heard about Stackless Python: Is it a separate option? I have no idea.

请告知.

推荐答案

我检查了一下,它看起来不错,但我还听说与线程不同,进程不能共享很多信息. ."

这只是部分正确.

线程是进程的一部分-线程琐碎地共享内存.这和帮助一样多的问题-两个相互随意的线程可能会覆盖内存并造成严重的问题.

Threads are part of a process -- threads share memory trivially. Which is as much of a problem as a help -- two threads with casual disregard for each other can overwrite memory and create serious problems.

但是,进程通过许多机制共享信息. Posix管道(a | b)表示进程a和进程b共享信息-a写入信息,b读取信息.这在很多方面都非常有效.

Processes, however, share information through a lot of mechanisms. A Posix pipeline (a | b) means that process a and process b share information -- a writes it and b reads it. This works out really well for a lot things.

操作系统将在创建进程时将您的进程分配给每个可用的核心.这在很多方面都非常有效.

The operating system will assign your processes to every available core as quickly as you create them. This works out really well for a lot of things.

无堆栈Python与该讨论无关-它速度更快,并且具有不同的线程调度.但是我不认为线程是实现此目的的最佳途径.

Stackless Python is unrelated to this discussion -- it's faster and has different thread scheduling. But I don't think threads are the best route for this.

我认为我的程序需要共享很多信息."

您应该先解决此问题.然后,确定如何围绕信息流构建流程. 管道"是非常容易和自然的.任何shell都会琐碎地创建管道.

You should resolve this first. Then, determine how to structure processes around the flow of information. A "pipeline" is very easy and natural to do; any shell will create the pipeline trivially.

服务器"是另一种体系结构,其中多个客户端进程获取信息和/或将信息放入中央服务器.这是共享信息的好方法.您可以使用WSGI参考实现来构建简单,可靠的服务器.

A "server" is another architecture where multiple client processes get and/or put information into a central server. This is a great way to share information. You can use the WSGI reference implementation as a way to build a simple, reliable server.

这篇关于多处理还是多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆