+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

为什么python不等待我的函数完成?

发布于2020-11-28 09:05     阅读(282)     评论(0)     点赞(19)     收藏(2)


0

1

2

3

4

因此,我使用tkinter和Python编写了一个应用程序,以通过串行控制某些设备。在这种情况下,我要控制恒温槽,并且有一个简单的功能可以等待,直到恒温槽达到我设定的温度为止wait_for_temp()我认为control_fcn()我在下面包含的内容将等到bc.wait_for_temp()调用完成后才开始,但是如果只是在其余部分中运行control_fcn()有人知道为什么吗?是因为打给我app.after()吗?

def control_fcn()
  bc = Bath_Controller()

  # set the temperature I want
  set_temp = 16.0
  bc.write_temp(set_temp)

  print("Commanding temp to " + str(set_temp))

  bc.turn_on()

  # wait for bath temp to be within .1 of the commanded temp
  # this function prints out a "Temperature reached. Carry on." statement when done
  bc.wait_for_temp(set_temp)

  print("I didn't wait for you friend.")

# in my Bath_Controller() class I have the following function
def wait_for_temp(self, set_temp):
  current_temp = self.read_temp()
  if (current_temp < set_temp + .05) and (current_temp > set_temp - .05):
    print("Temperature reached. Carry on.")
    return
  else:
    print("Waiting for temperature equilibration.\n Current temp is:")
    # app here is my root window in tkinter
    app.after(10000, lambda: self.wait_for_temp(set_temp))

-更新-

因此,感谢一些有用的答案,我终于有了一种合理且更可维护的方式来等待我的串行设备完成其任务,而又不冻结GUI。我不得不草拟一个状态机,但如果有人好奇,这是我当前的工作代码,涉及更改一些压力阀,更改温度,等待该温度更改,等待用户从管线中取样水,以及在多个温度和压力下执行此操作。

from collections import deque

def calibrate_master():

    bc = Bath_Controller()
    vc = Valve_Controller()
    sc = Sampling_Controller()

    pressure_queue  = deque([0, 1, 2, 3, 4, 5])
    temp_queue      = deque([6, 4, 2])

    app.ready_for_pres_change   = True  # this starts True, and is reset True after last temp and sampling
    app.ready_for_temp_change   = False # this is set after pres change and after sampling
    app.waiting_for_temp        = False # this is set true when waiting for temp
    app.waiting_for_sample      = False # this is set true after temp reached and before sampling is done

    calibrate_slave(app, bc, vc, pc, pressure_queue, temp_queue)

def calibrate_slave(app, bc, vc, pc, pressure_queue, temp_queue):

    if app.ready_for_pres_change:
        vc.set_pressure(pressure_queue.popleft())
        app.ready_for_pres_change = False
        app.ready_for_temp_change = True
    elif app.ready_for_temp_change:
        bc.set_temp(temp_queue.popleft())
        app.ready_for_temp_change = False
        app.waiting_for_temp = True
    elif app.waiting_for_temp:
        bc.check_temp() # check_temp() will change waiting_for_temp to False and waiting_for_sample to True once temp is met
    elif app.waiting_for_sample:
        sc.check_sampling() # this checks if the user has pressed a button to indicate sampling complete
        if not app.waiting_for_sample:
            if temp_queue: # if there are temps left in the temp queue keep running through at same temp
                app.ready_for_temp_change = True
            elif pressure_queue: # if there are pressures left refill temp queue and move on to next pres
                temp_queue = deque([6, 4, 2])
                app.ready_for_pres_change = True
            else: # if there are not temps and no pressures left the calibration is complete
                break 

    # check back every five seconds
    app.after(5000, lambda: calibrate_slave(app, bc, vc, pc, pressure_queue, temp_queue))

解决方案


它确实等待wait_for_temp完成。wait_for_temp将几乎立即返回。调用after不会阻止它返回。相反,调用after允许它返回而无需等待。

编写GUI与编写非GUI程序不同。GUI已经处于永久等待状态。您无需编写本身等待的功能,而需要编写可以响应事件的代码。

例如,您可以创建一个<<TemperatureReached>>事件,当温度达到给定值时可以触发事件。然后,您可以在事件触发时绑定要调用的函数。

它看起来像这样:

def control_fcn()
    bc = Bath_Controller()

    # set the temperature I want
    set_temp = 16.0
    bc.write_temp(set_temp)

    # arrange for notifyUser to be called when the target
    # temperature has been reached
    app.bind("<<TemperatureReached>>", self.notifyUser)

    # periodically check the temp
    self.check_temp(set_temp)

def notifyUser(event):
    print("Temperature reached")

def check_temp(self, set_temp):
    current_temp = self.read_temp()
    if (current_temp < set_temp + .05) and (current_temp > set_temp - .05):
        # temperature reached; send an event
        app.event_generate("<<TemperatureReached>>")
    else:
        # temperature not reached. Check again in 10 seconds
        app.after(10000, self.check_temp, set_temp)

0

1

2

3

4

5

6



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/634471/68a712266c19d6f2cebd/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

19 0
收藏该文
已收藏

评论内容:(最多支持255个字符)