Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Следующий пример показывает, как к потоку приаттачить функцию через вызов функции
- """
- import threading
- import time
- def clock(interval):
- while True:
- print("The time is %s" % time.ctime())
- time.sleep(interval)
- t = threading.Thread(target=clock, args=(15,))
- t.daemon = True
- t.start()
- """
- Пример на создание потока через вызов класса: в конструкторе обязательно нужно вызвать конструктор базового класса. Для запуска потока нужно выполнить метод start() объекта-потока, что приведет к выполнению действий в методе run():
- """
- import threading
- import time
- class ClockThread(threading.Thread):
- def __init__(self,interval):
- threading.Thread.__init__(self)
- self.daemon = True
- self.interval = interval
- def run(self):
- while True:
- print("The time is %s" % time.ctime())
- time.sleep(self.interval)
- t = ClockThread(15)
- t.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement