Advertisement
Woobinda

Модуль threading

Sep 14th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. """
  2. Следующий пример показывает, как к потоку приаттачить функцию через вызов функции
  3. """
  4.  
  5. import threading
  6. import time
  7.  
  8. def clock(interval):
  9.     while True:
  10.         print("The time is %s" % time.ctime())
  11.         time.sleep(interval)
  12. t = threading.Thread(target=clock, args=(15,))
  13. t.daemon = True
  14. t.start()
  15.  
  16.  
  17.  
  18. """
  19. Пример на создание потока через вызов класса: в конструкторе обязательно нужно вызвать конструктор базового класса. Для запуска потока нужно выполнить метод start() объекта-потока, что приведет к выполнению действий в методе run():
  20. """
  21.  
  22. import threading
  23. import time
  24.  
  25. class ClockThread(threading.Thread):
  26.     def __init__(self,interval):
  27.         threading.Thread.__init__(self)
  28.         self.daemon = True
  29.         self.interval = interval
  30.     def run(self):
  31.         while True:
  32.             print("The time is %s" % time.ctime())
  33.             time.sleep(self.interval)
  34. t = ClockThread(15)
  35. t.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement