简体   繁体   English

Python多个循环同时进行

[英]Python multiple loops at the same time

I know that it is not possible to run multiple loops at the same time in Python. 我知道在Python中同时运行多个循环是不可能的。 Anyhow, what I need to achieve is that I have one loop running reading loads of sensor data, every 0.25 seconds. 无论如何,我需要实现的是我有一个循环运行读取传感器数据的负载,每0.25秒。 At the same time I have signal devices running in parallel that need to send signals every 3 seconds. 同时,我有并行运行的信号设备需要每3秒发送一次信号。 My question is what way is best practice to achieve this? 我的问题是,实现这一目标的最佳做法是什么?

Does it make sense to write two scripts and run them in parallel? 编写两个脚本并并行运行它们是否有意义? Does it make sense to use threading? 使用线程是否有意义? Is there any other possibility in order to make this work? 为了使这项工作还有其他可能性吗?

I would be greatful for code samples. 我会很高兴代码样本。

Thank you! 谢谢!

Edit: Both loops are absolutely independent. 编辑:两个循环绝对独立。 So, let's say while script 1 is running, reading the sensor data, when one of the sensors received a value < 300, it should run script 2 which will send the signals. 因此,假设当脚本1运行时,读取传感器数据,当其中一个传感器接收到值<300时,它应该运行脚本2,它将发送信号。 At the same time when the sensors data gets > 300 it should stop script 2. 在传感器数据> 300的同时,它应该停止脚本2。

"Python multiple loops at the same time. I know that it is not possible [...]" - this looks really funny. “同时Python多个循环。我知道这是不可能的[...]” - 这看起来很有趣。

It is possible to run two loops at the same time, exactly how you described it. 可以同时运行两个循环,正如您所描述的那样。 And both ways make much sense, depending on what do you actually need and want. 这两种方式都很有意义,取决于你实际需要和想要的东西。 If the tasks are completely independent you should run it as two scripts. 如果任务完全独立,则应将其作为两个脚本运行。 If you need those two loops to realize one task and it makes sense for them to be in one file you can use multiprocessing . 如果你需要这两个循环来实现一个任务,并且它们在一个文件中是有意义的,你可以使用multiprocessing

Tested for python 2.7.5+ and 3.3.2+. 经测试为python 2.7.5+和3.3.2+。

Here is some minimal example: 这是一些最小的例子:

from multiprocessing import Process
import time

def f(name):
    print('hello', name)
    time.sleep(10)

def d(name):
    print('test2', name)
    time.sleep(10)

if __name__ == '__main__':
    p1 = Process(target=f, args=('bob',))
    p2 = Process(target=d, args=('alice',))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

Script runs for 10s and both strings are printed right away, which means everything works. 脚本运行10秒,两个字符串立即打印,这意味着一切正常。

time python3 ./process.py 
hello bob
test2 alice

real  0m10.073s
user  0m0.040s
sys   0m0.016s

It is also possible by running multiple scripts and some as .pyw for convenience and having them exchange information by UDP sockets. 为方便起见,还可以通过运行多个脚本和一些.pyw来让它们通过UDP套接字交换信息。 Note 127.0.0.1 is to send to yourself under ANY circumstance. 注释127.0.0.1是在任何情况下发送给您自己。 Also for port, just make sure no other programs use the port you use. 同样对于端口,只需确保没有其他程序使用您使用的端口。 As in other programs, I mean ANY program that uses ports or even basic router settings. 和其他程序一样,我的意思是任何使用端口甚至基本路由器设置的程序。

Sample (send) 样品(发送)

import os
from sockets import *
host = "ip"
port = "9000"
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
data = "Random Text"
send = data.encode("ascii")
UDPSock.sendto(send, addr)
UDPSock.close()

Sample (Receive) 样品(接收)

import os
from socket import *
host = ""
port = 9000
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(addr)
(data, addr) = UDPSock.recvfrom(1024)#1024 is MAX bytes to receive
data = data.decode('ascii')
UDPSock.close()

You can use these to run separate loops and tell what to do from two separate programs. 您可以使用它们来运行单独的循环,并告诉您从两个单独的程序中执行的操作。

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

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