我怎么能同时运行一个无限循环的瓶子?

2024-09-22 10:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想逃跑瓶子.py作为脚本中的一个过程,但我很沮丧,因为它并不像我预期的那样工作。 这是我的脚本的一个假设/简短表示。我尽可能多地添加评论。在

在弹球.py在

import serial
from bottle import route, run, template, request
import multiprocessing
import logging
import datetime


#Here are the most important variables which will be used in the entire script
global lastcommand #The last command received from bottle web interface
global status #What is the current status

lastcommand = 'Go Home' #Options: Start Drawing, Go Home, Next Drawing
status = 'home' #Options: home, homing (means going home), drawing

def log_this(level, msg)
    #a special log function based on logging library for debugging
    pass # <- details are not important, it is working fine

now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %H:%M")
info = {'title' : 'MAGBALL', 'time': timeString, 'lastcommand': lastcommand, 'status': status} #These are passed to main.tpl
@route('/', method='GET')
@route('/', method='POST')
def index(): #<- This works fine, passing the info to the template file (main.tpl) in views directory
    global lastcommand #<- Isn't this referring to the global lastcommand declared at the beginning?
    if request.method == 'POST':
        lastcommand = request.forms.get("submit") #<- This works fine and lastcommand changes when the button on the web page is clicked    
        log_this('info', (lastcommand + ' clicked on web interface')) #<- I can see in the logs which button is clicked, so, this should mean 
                                                                      #that lastcommand (supposedly the global one) was changed successfully

    return template('main.tpl', info)

p_bottle = multiprocessing.Process(target=run, kwargs=dict(host='0.0.0.0', port=80, debug='True')) #<- I start bottle as a process because I think I have to
p_bottle.daemon = True

def draw_pattern() #<- This function can easily run for hours which is fine/normal for the purpose
                   #Unless lastcommand is not equal to 'Start Drawing' this should run forever
    global status
    global lastcommand
    status = 'drawing' #<- I think this should also refer to the global lastcommand declared at the beginning
    #Opens a random file from a specific directory, 
    for filename in shuffled_directory:
        #reads the file line by line and feeds it to the serial port
        .
        .
        if lastcommand != 'Start Drawing': #<- This never works! Although I see in the logs that lastcommand was changed by index() function
            log_this('warning', 'Last command changed during line feeding.')
            break

def go_home()
    global status
    status = 'homing' #<- I think this should also refer to the global lastcommand declared at the beginning
    #Send some commands to serial port and after that change the value of status
    .
    .
    status = 'home'

while true: #This is the infinite loop that needs to run forever
    global status #Don't these variables also refer to the global variables declared at the beginning? 
    global lastcommand
    p_bottle.start() #The bottle.py web interface runs successfully
    #The following while, if etc never runs. I think this "while true:" statement never gets notified
    #that the values of lastcommand and/or status has changed
    while lastcommand == 'Start Drawing':   
        if status == 'home':
            draw_pattern()
        if status == 'homing':
            time.sleep(5)
        if status == 'drawing':
            pass
    while lastcommand == 'Next Drawing':
        if status == 'home':
            draw_pattern()
        if status == 'homing':
            time.sleep(5)
        if status == 'drawing':
            go_home()
            draw_pattern()
    while lastcommand == 'Go Home':
        if status == 'home':
            pass
        if status == 'homing':
            pass
        if status == 'drawing':
            go_home()

在主.tpl在

^{pr2}$

以下是发生的事情:
瓶子过程运行良好(网页是可访问的),当我单击网页上的按钮时,我在日志中看到,lastcommand的值被web界面更改了。在

但是,末尾的无限循环(while true:)永远不会收到lastcommand值已更改的通知。例如,如果status=home和last命令从web界面更改为“Start Drawing”,则它不会执行draw_pattern()。它什么也没做。在

我试图将所有内容放在def index()函数中的return template('main.tpl', info)行之前,但是代码运行,但是web界面等待代码完成才能启动。由于draw_pattern()持续数小时,因此return template('main.tpl', info)行实际上不运行,网页也不可访问。在

我还试图将def index()作为一个普通函数运行,但是当我这样做时,唯一运行的就是web接口(def index())。在

那么,如何通知while循环lastcommand和/或status的值已更改?在


Tags: thetoinfowebbottlehomeifis
1条回答
网友
1楼 · 发布于 2024-09-22 10:19:58

对于任何感兴趣的人,我已经用线程而不是多处理来解决这个问题。 所以不要用这个:

p_bottle = multiprocessing.Process(target=run, kwargs=dict(host='0.0.0.0', port=80, debug='True')) 
p_bottle.daemon = True

我用过:

^{pr2}$

现在线程中的函数可以成功地改变全局变量,并立即被无限的while true循环获取。 谢谢大家的建议。

相关问题 更多 >