在python中同时运行交互函数

在python中同时运行交互函数,python,python-3.x,python-multiprocessing,python-multithreading,Python,Python 3.x,Python Multiprocessing,Python Multithreading,所以我想造一个能自动驾驶的机器人。为此,我需要机器人向前行驶,同时检查距离。如果距离小于首选距离,则停止向前移动。到目前为止,我已经在下面编写了这段代码,但它似乎没有同时运行,而且它们也没有交互。我怎样才能使这两个功能真正相互作用呢。如果需要更多信息,我很乐意提供给您。谢谢 from multiprocessing import Process from TestS import distance import Robot import time constant1 = True min_di

所以我想造一个能自动驾驶的机器人。为此,我需要机器人向前行驶,同时检查距离。如果距离小于首选距离,则停止向前移动。到目前为止,我已经在下面编写了这段代码,但它似乎没有同时运行,而且它们也没有交互。我怎样才能使这两个功能真正相互作用呢。如果需要更多信息,我很乐意提供给您。谢谢

from multiprocessing import Process
from TestS import distance
import Robot
import time

constant1 = True
min_distance = 15

def forward():
    global constant1:
    robot.forward(150)                       #forward movement, speed 150
    time.sleep(2)

def distance_check():
    global constant1
    while constant1:
        distance()                           #checking distance
        dist = distance()
        return dist
        time.sleep(0.3)

        if dist < min_distance:
            constant1 = False
            print 'Something in the way!'
            break

def autonomy():                              #autonomous movement
    while True:
        p1 = Process(target=forward)         
        p2 = Process(target=distance_check)
        p1.start()                           #start up 2 processes
        p2.start()
        p2.join()                            #wait for p2 to finish
来自多处理导入进程的

从测试导入距离
进口机器人
导入时间
constant1=True
最小距离=15
def forward():
全球气候变化1:
机器人。向前(150)#向前移动,速度150
时间。睡眠(2)
def距离检查()
全局常数1
而constant1:
距离()#检查距离
距离=距离()
返回区
睡眠时间(0.3)
如果距离<最小距离:
constant1=False
打印“挡道的东西!”
打破
def autonomy():#自主运动
尽管如此:
p1=过程(目标=前进)
p2=过程(目标=距离检查)
p1.启动()#启动2个进程
p2.start()
p2.join()#等待p2完成

因此,您发布的代码存在一些严重问题。首先,您不希望距离检查过程结束,因为它正在运行while循环。您不应该执行p2.join(),也不应该在while循环中一直启动新进程。你在这里混合了太多的做事方式——要么两个孩子永远跑,要么他们每人跑一次,而不是混合

但是,主要的问题是原始流程无法与原始流程通信,即使是通过
全局
(除非您做更多的工作)。线程更适合这个问题

distance\u check()
函数中还有一个
return
,因此该语句下面的代码不会被执行(包括
sleep
,以及
constant1
的设置(应该有一个更好的名称)

总而言之,我想你想要这样的东西:

from threading import Thread
from TestS import distance
import Robot
import time

can_move_forward = True
min_distance = 15


def move_forward():
    global can_move_forward
    while can_move_forward:
        robot.forward(150)
        time.sleep(2)
        print('Moving forward for two seconds!')


def check_distance():
    global can_move_forward
    while True:
        if distance() < min_distance:
            can_move_forward = False
            print('Something in the way! Checking again in 0.3 seconds!')
        time.sleep(0.3)


def move_forward_and_check_distance():
    p1 = Thread(target = move_forward)
    p2 = Thread(target = check_distance)
    p1.start()
    p2.start()
从线程导入线程
从测试导入距离
进口机器人
导入时间
可以向前移动吗
最小距离=15
def向前移动()
全球经济能向前发展吗
当你能前进时:
机器人前进(150)
时间。睡眠(2)
打印('向前移动两秒钟!')
def检查距离():
全球经济能向前发展吗
尽管如此:
如果距离()小于最小距离:
能否向前移动=错误
打印('有东西挡住了!0.3秒后再次检查!')
睡眠时间(0.3)
def向前移动并检查距离()
p1=线程(目标=向前移动)
p2=螺纹(目标=检查距离)
p1.开始()
p2.start()
由于您在标记中指定了python-3.x,因此我还更正了您的
打印


显然,我无法检查它是否能按您的要求工作,因为我没有您的机器人,但我希望这至少有点帮助。

您的多处理解决方案的一个问题是,
距离检查
返回并停止

:

生产商应不断检查移动是否安全

def can_move(target, min_distance = 15):
    '''Continually check for obstacles'''
    while distance() > min_distance:
        target.send(True)
        print('check distance')
    target.close()
发电机/协同程序,使用安全移动信号,并根据需要改变机器人的速度

@consumer
def forward():
    try:
        while True:
            if (yield):
                robot.forward(150)
    except GeneratorExit as e:
        # stop the robot
        robot.forward(0)
机器人的速度变化应与障碍物传感器产生距离的速度一样快。机器人将向前移动,直到接近某个物体,然后停止,然后全部关闭。通过在
向前
可以移动
中稍微调整逻辑,可以改变行为,使发电机/协同程序保持运行,但不会停止结束零速指令,只要前方有物体,当物体让路(或机器人转弯)时,它将再次开始移动

用法:

>>> 
>>> robot = Robot('Foo')
>>> can_move(forward())
distance? 100
    Foo forward speed is 150
check distance
distance? 50
    Foo forward speed is 150
check distance
distance? 30
    Foo forward speed is 150
check distance
distance? 15
    Foo forward speed is 0
    Robot 0 stopped
>>>


虽然这在Python 3.6中有效,但它基于对生成器和协同程序的可能过时的概念/理解。在Python 3+中添加了一些
async
,可能会有不同的方法来实现这一点。

您可能希望在线程之间使用a进行通信。您意识到线程不会同时运行吗?@wwii提问者似乎想使用globals。对于这样简单的事情,队列似乎有点过头了。我同意线程不会同时运行,但对于这种情况,它应该是同时运行的。感谢您的解决方案!我同意constant1应该有一个更好的名称。我正在尝试使用globals的一些东西,我认为它们很合适为了这份工作。我也会尝试一下你的代码!我真的很喜欢你的解决方案,因为它很好而且简单。但是它有一个缺陷。当运行代码时,它达到了can_move_forward=False的程度。之后它会短暂停止,然后再向前移动一段很短的时间,最后停止。我想对b没有什么可做的因为线程不是同时运行的?@NickHonings我只是用
检查距离
修复了一个bug(循环应该一直运行,如果
可以向前移动
,不只是运行)。我不知道这可能会导致问题,但也许可以尝试一下。此外,
睡眠时间可能过长-线程应该很快关闭,因此代码中可能存在延迟。你能解释一下为什么它们不
同时运行或交互吗?你怎么知道?首先,非常感谢您的回复!我正试图了解您的代码,因为我只是一个初学者。但我开始了解您的方法,我一定会尝试!
def can_move(target, min_distance = 15):
    '''Continually check for obstacles'''
    while distance() > min_distance:
        target.send(True)
        print('check distance')
    target.close()
@consumer
def forward():
    try:
        while True:
            if (yield):
                robot.forward(150)
    except GeneratorExit as e:
        # stop the robot
        robot.forward(0)
>>> 
>>> robot = Robot('Foo')
>>> can_move(forward())
distance? 100
    Foo forward speed is 150
check distance
distance? 50
    Foo forward speed is 150
check distance
distance? 30
    Foo forward speed is 150
check distance
distance? 15
    Foo forward speed is 0
    Robot 0 stopped
>>>