【问题标题】:Scheduled task in a service with Kivy / python for androidKivy / python for android服务中的计划任务
【发布时间】:2021-09-18 01:50:57
【问题描述】:

我想使用 Clock.schedule_interval(mycallback,dt) 在后台运行的服务中实现计划任务。当我在我的服务中实现它时,执行此行之前和之后的代码,但从不调用回调。就像代码被忽略了,我没有任何错误。 我阅读了许多论坛和文档,但没有找到任何内容。 感谢您的帮助!

我刚刚修改了我在这里找到的示例:https://github.com/tshirtman/kivy_service_osc

我的service.py

'p4a example service using oscpy to communicate with main application.'
from random import sample, randint
from string import ascii_letters
from time import localtime, asctime, sleep

from oscpy.server import OSCThreadServer
from oscpy.client import OSCClient

from kivy.clock import Clock

CLIENT = OSCClient('localhost', 3002)


def ping(*_):
    'answer to ping messages'
    CLIENT.send_message(
        b'/message',
        [
            ''.join(sample(ascii_letters, randint(10, 20)))
            .encode('utf8'),
        ],
    )


def send_date(dt):
    'send date to the application'
    print('debug2')
    CLIENT.send_message(
        b'/date',
        [asctime(localtime()).encode('utf8'), ],
    )
   

if __name__ == '__main__':
    SERVER = OSCThreadServer()
    SERVER.listen('localhost', port=3000, default=True)
    SERVER.bind(b'/ping', ping)
    Clock.schedule_clock(send_date,1)    
    while True:
        sleep(1)
        print("debug")

【问题讨论】:

    标签: python android service kivy clock


    【解决方案1】:

    要获取错误并调试您的代码,请尝试该操作

    from random import sample, randint
    from string import ascii_letters
    from time import localtime, asctime, sleep
    
    from oscpy.server import OSCThreadServer
    from oscpy.client import OSCClient
    
    from kivy.clock import Clock
    
    CLIENT = OSCClient('localhost', 3002)
    
    
    def ping(*_):
        'answer to ping messages'
        CLIENT.send_message(
            b'/message',
            [
                ''.join(sample(ascii_letters, randint(10, 20)))
                .encode('utf8'),
            ],
        )
    
    
    
    def send_date(dt):
     try:
        'send date to the application'
        print('debug2')
        CLIENT.send_message(
            b'/date',
            [asctime(localtime()).encode('utf8'), ],
        )
     except Exception as myerror:
      print(myerror)
    
    if __name__ == '__main__':
        SERVER = OSCThreadServer()
        SERVER.listen('localhost', port=3000, default=True)
        SERVER.bind(b'/ping', ping)
        Clock.schedule_clock(send_date,1)    
        while True:
            sleep(1)
            print("debug")
    

    【讨论】:

      猜你喜欢
      • 2011-05-22
      • 2011-02-18
      • 2016-02-10
      • 2010-09-28
      • 2011-07-05
      • 2018-04-04
      • 1970-01-01
      相关资源
      最近更新 更多