在python中独立运行多个while true循环 [英] Running multiple while true loops independently in python
问题描述
基本上我的代码中有 2 个while True:
"循环.两个循环都在最后.但是,当我运行代码时,只有第一个 while True:
循环运行,而第二个循环被忽略.
Essentially I have 2 "while True:
" loops in my code. Both of the loops are right at the end. However when I run the code, only the first while True:
loop gets run, and the second one gets ignored.
例如:
while True:
print "hi"
while True:
print "bye"
在这里,它会连续打印 hi,但根本不会打印 bye(实际代码有一个 tracer.execute()
用于一个循环,另一个是监听端口,它们两者都独立工作).
Here, it will continuously print but wont print bye at all (the actual code has a tracer.execute()
for one loop, and the other is listening to a port, and they both work on their own).
有没有办法让两个循环同时独立工作?
推荐答案
是的.
一种让两个循环同时独立工作的方法:
您最初的惊讶与性质有关,即有限状态自动机的实际工作方式.
Yes.
A way to get both loops to work at the same time independently:
Your initial surprise was related to the nature, how Finite-State-Automata actually work.
[0]: any-processing-will-always-<START>-here
[1]: Read a next instruction
[2]: Execute the instruction
[3]: GO TO [1]
抽象指令流以纯[SERIAL]
方式,一个接一个.自从图灵叔叔之后,CPU 就没有别的办法了.
The stream of abstract instructions is being executed in a pure-[SERIAL]
manner, one after another. There is no other way in the CPU since uncle Turing.
您希望同时独立运行更多指令流被称为[CONCURRENT]
流程调度.
Your desire to have more streams-of-instructions run at the same time independently is called [CONCURRENT]
process-scheduling.
阅读一种较弱的形式,仅使用基于线程的并发(由于特定于 Python 的 GIL 锁定,它在物理硬件上作为 [CONCURRENT]
-处理,但 GIL-interleaving(这是故意实现的一种非常廉价的碰撞形式- 避免每种情况,这 [CONCURRENCY]
可能会引入)最后交错每个(现在)[CONCURRENT]
-streams,以便主要避免同时对任何 Python 对象的访问发生冲突.如果您对这个一次性执行一次指令没问题(并循环他们的 GIL 步进执行的实际顺序),您可以生活在一个安全且无碰撞的世界中.
Read about a weaker form, using just a thread-based concurrency ( which, due to a Python-specific GIL-locking, yet executes on the physical hardware as a [CONCURRENT]
-processing, but GIL-interleaving ( which was knowingly implemented as a very cheap form of a collision-avoidance for each and every case, that this [CONCURRENCY]
might introduce ) will finally interleave each of the ( now ) [CONCURRENT]
-streams, so as to principally avoid colliding access to any Python object at the same time. If you are fine with this execute-just-one-instruction-stream-fragment-at-a-time ( and round-robin their actual order of GIL-stepped execution ), you can live in a safe and collision-free world.
Python 可能会使用的另一个工具是 joblib.Parallel()( joblib.delayed() )
,您必须在其中掌握更多内容才能制作这些(现在是一组完全生成的子进程,每个(是的,每个)都有一个完整的 python 状态副本+所有变量(阅读:生成它们需要大量的时间和内存)并且没有相互协调).
Another tool, Python may use, is the joblib.Parallel()( joblib.delayed() )
, where you will have to master a bit more things to make these ( now a set of fully spawned subprocesses, each ( yes, each ) having a full-copy of python-state + all variables ( read: a lot of time and memory needed to spawn 'em ) and no mutual coordination ).
因此决定哪种形式适合您的用例类型,并且最好检查新的 Amdahl仔细重新制定法律(对分布式或并行的成本的影响)
So decide about which form is just-enough for the kind of your use-case, and better check the new Amdahl's Law re-formulation carefully ( implications on costs of going distributed or parallel )
这篇关于在python中独立运行多个while true循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!