线程问题 [英] Problem in threading

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

问题描述

我已经编写了一个代码来计算使用线程之前和之后的执行时间的差异

...

I have written a code to figure out the difference in excecution time
of a func before and after using threading...

展开 < span class =codeDivider> | 选择 | Wrap | Line数字

推荐答案

Gurpreet Sachdeva< gu *************** @ gmail.com>写道:
Gurpreet Sachdeva <gu***************@gmail.com> wrote:
for n in nloops:#wait all all
threads [i] .join
for i in nloops: # wait for all
threads[i].join




Missing ('加入'后)。

Alex



Missing () after ''join''.
Alex


Gurpreet Sachdeva写道:
Gurpreet Sachdeva wrote:
时间的差异并不多......
我们如何通过线程最好地优化我们的任务...请帮助...
Also the difference of time is not much...
How do we best optimize our task by using threads... please help...




对于大多数人来说将处理拆分为单独的线程的任务将导致完成任务的总时间增加



可能导致运行时间减少的唯一时间是任务

所花费的时间并不完全取决于CPU所用的时间,或者当

涉及多个CPU时。


不幸的是后一种情况并没有被Python处理好,所以不要指望

多个CPU',以帮助加速多线程Python程序非常多的b $ b。这留下了前一种情况:如果你的任务必须停止并等待

其他事情发生(例如,要从网络读取数据,或者要从中读取
)一个光盘文件),然后将它分成多个线程可能允许

等待与有用的处理重叠,这可能导致处理时间整体减少

。 />

线程的良好使用是提高系统的响应能力。例如

如果你确保GUI处理发生在一些单独的线程上,那么你可以确保GUI仍然是

在计算运行时响应。它不会让计算完成得更快(事实上它可能会更慢),但用户将会更高兴。
仍然更快乐。类似地,网络应用程序通常是多线程的,因此所有请求都有公平的机会完成,而不是必须等待最慢的完成才能运行。


如果你有多个处理器可用并希望加速Python

程序那么你可能需要查看多个进程而不是

多个线程。或者你可以通过在C中重写内部循环并释放

解释器锁来移动部分处理器来解决Python环境的问题,但这通常不是最好的选项。



For most tasks splitting the processing into separate threads will result
in an increase in the total time to complete the task. The only times when
it may result in a decrease in the running time are when the time the task
takes does not entirely depend on the time taken by the CPU, or when
multiple CPU''s are involved.

Unfortunately the latter case isn''t handled well by Python, so don''t expect
multiple CPU''s to help speed up a multi-threaded Python program by very
much. That leaves the former case: if your task has to stop and wait for
something else to happen (e.g. data to be read from a network, or to be
read from a disc file), then splitting it into multiple threads may allow
the waits to be overlapped with useful processing which could result in an
overall decrease in processing time.

A good use of threads is to improve responsiveness of a system. For example
if you ensure that GUI processing happens on a separate thread from some
CPU intensive computation then you can ensure that the GUI remains
responsive while the computation is running. It won''t make the computation
complete any faster (in fact it will probably be slower), but the user will
remain happier. Similarly network applications are usually multi-threaded
so that all requests get a fair chance to complete instead of having to
wait for the slowest to complete before others can run.

If you do have multiple processors available and want to speed up a Python
program then you probably have to look at multiple processes rather than
multiple threads. Alternatively you could move parts of the processing out
of the Python environment by rewriting inner loops in C and releasing the
interpreter lock, but that isn''t usually the best option.


Duncan Booth< du ********** @ invalid.invalid>写道:
Duncan Booth <du**********@invalid.invalid> writes:
这留下了前一种情况:如果你的任务必须停止并等待其他事情发生(例如,要从网络读取数据,或者<从光盘文件中读取,然后将其分成多个线程
可能允许等待与有用的处理重叠,这可能导致处理时间整体减少。
That leaves the former case: if your task has to stop and wait for
something else to happen (e.g. data to be read from a network, or to
be read from a disc file), then splitting it into multiple threads
may allow the waits to be overlapped with useful processing which
could result in an overall decrease in processing time.




如果您使用类似Unix的系统,如果你使用async I / / b
这样做,那么你会使用更少的阿司匹林O和选择模块。如果您使用的是Windows *,那么您可以选择的限制使其更少

有用。 Python的线程模型非常原始。你得到的是C / /
模型(容易出错),Java模型(在2.4中,还有

容易出错)或队列。队列不容易出错,但会产生与你选择的行为相同的行为:启动I / O,在I / O时执行计算

正在进行,阻止直到I / O完成。


< mike

-

Mike Meyer< mw *@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



If you''re on a Unix-like system, you''ll use less aspirin if you do
this kind of thing with async I/O and the select module. If you''re on
Windows, the restrictions on what you can select on make it much less
useful. Python''s threading models is pretty primitive. You get the C
model (which is error-prone), the Java model (in 2.4, and also
error-prone), or Queues. Queues aren''t error-prone, but result in the
same kind of behavior as you get with select: start I/O, do computing
while I/O is going on, block until I/O is complete.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


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

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