Как я могу запустить функцию навсегда? - PullRequest
0 голосов
/ 06 февраля 2020

Я пытаюсь запустить функцию слушателя навсегда, например, я хочу в любое время, когда в потоке появляется новая информация, которую он автоматически обновляет в списке. Любая идея, как это сделать, будет оценена.

class Notifications(Screen):
   notificationslist = ObjectProperty(None)

def listener(self, event = None):
       notifications_screen = self.manager.get_screen('notif')
       print(event.event_type)  # can be 'put' or 'patch'
       print(event.path)  # relative to the reference, it seems
       print(event.data)  # new data at /reference/event.path. None if deleted
       notifications = event.data
       if notifications.items() == None:
           return
       else:
           for key, value in notifications.items():
               thevalue = value
               notifications_screen.notificationslist.adapter.data.extend([value[0:17] + '\n' + value[18:]])
               print(thevalue)
               id = (thevalue[thevalue.index("(") + 1:thevalue.rindex(")")])
               print(id)

1 Ответ

3 голосов
/ 06 февраля 2020

Если вы хотите, чтобы функция выполнялась вечно, но не мешала вам выполнять другие функции, тогда вы можете использовать threads.

. Вот пример с example_of_process, который выполняется вечно, а затем основная программа с time.sleep(3)

import threading
import time

def example_of_process():
    count = 0
    while True:
        print("count", count)
        count += 1
        time.sleep(1)

thread_1 = threading.Thread(target=example_of_process)
thread_1.daemon = True # without the daemon parameter, the function in parallel will continue even if your main program ends
thread_1.start()

# Now you can do anything else. I made a time sleep of 3s, otherwise the program ends instantly
time.sleep(3)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...