代码在Thonny中有效,但在终端中无效。 RPI

时间:2019-08-18 16:58:35

标签: python python-3.x multithreading terminal multiprocessing

我是Python的新手,并尝试使用Raspberry在我的RPI上编写一个小脚本。 我的问题是代码在Thonny中运行良好,但是当我在LXTerminal中加载它时,某些功能无法正常工作。

当我在Thonny中进行计算并且按下button21 30秒钟时,有效(回合((有效时间/刷新)* 100))值将为50%,但是当我在终端中有效地执行相同操作时,它将为0,但是如果我完全不按button21,则有效值为100。

就像有效计时器只是发送“全部价值”或什么都不发送。

有人有主意吗?

!/usr/bin/env python

import pyodbc
import time
from multiprocessing import Process, Value
from gpiozero import Button
from threading import Thread

button21 = Button(21)
button20 = Button(20)

cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER=****;PORT=1433;DATABASE=****;UID=****;PWD=****;TDS_Version=8.7;')
counter=Value('h', 0)
x=Value('h', 0)
refresh = 10
elapsed_stoptime = 0
effectivelytime = 1
effectively = 0
precounter = 0

def function1():
    while True:
        global effectivelytime
        global effectively
        global precounter
        effectivelytime = x.value
        effectively = round((effectivelytime / refresh)*100)
        if counter.value == precounter:
            effectively = 0            
        cursor = cnxn.cursor()
        cursor.execute('INSERT INTO ****.dbo.****(counter, effectively) VALUES ({},{})'.format (counter.value, effectively,))
        cnxn.commit()
        cursor.close()
        print("Printed To DB - Counter = ", counter.value, ", Effectively = ", effectively,)   
        x.value = 0
        precounter = counter.value
        time.sleep(refresh)

def function2():
    button21.wait_for_release()
    counter.value = counter.value + 1
    print (counter.value, "Cykel")

def effectivelytimer():
    while True:
        if button20.is_pressed:
            x.value = x.value + 1
            time.sleep(1)
        else:
            time.sleep(1)


def trigcounter():
    t2 = Thread(group=None,target=function2)
    t2.start()

button21.when_pressed = trigcounter

if __name__ == '__main__':
    t1 = Thread(group=None,target=function1)
    t3 = Thread(group=None,target=effectivelytimer)
    t1.start()
    t3.start()

我根本没有收到任何错误消息。

1 个答案:

答案 0 :(得分:0)

Thonny使用Python3。脚本(!/usr/bin/env python)的shebang行指的是Python2。尝试将!/usr/bin/env python更改为!/usr/bin/env python3