
本文共 7277 字,大约阅读时间需要 24 分钟。
������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������CPU������������������������������������������������������������������������������������������CPU���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������-- ���������������������������
���������������������������
Python3������������������������������������
thread ��������������������������������������� threading ������������������������ Python3 ������������������"thread" ���������������������������Python3 ��� thread ������������ "_thread"���
������������Python������������������������������������������������������������������������������
������������������ _thread ������������ start_new_thread()������������������������������������������
_thread.start_new_thread ( function, args[, kwargs] )
���������������
- function - ���������������
- args - ������������������������������,���������������tuple���������
- kwargs - ���������������
���������
#!/usr/bin/python3import _threadimport time# ���������������������������def print_time(threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print("%s: %s" % (threadName, time.ctime(time.time())))# ������������������try: _thread.start_new_thread(print_time, ("Thread-1", 2, )) _thread.start_new_thread(print_time, ("Thread-2", 4, ))except: print("Error: ������������������")while 1: pass
���������������������������������������
Thread-1: Wed Apr 6 11:36:31 2016Thread-1: Wed Apr 6 11:36:33 2016Thread-2: Wed Apr 6 11:36:33 2016Thread-1: Wed Apr 6 11:36:35 2016Thread-1: Wed Apr 6 11:36:37 2016Thread-2: Wed Apr 6 11:36:37 2016Thread-1: Wed Apr 6 11:36:39 2016Thread-2: Wed Apr 6 11:36:41 2016Thread-2: Wed Apr 6 11:36:45 2016Thread-2: Wed Apr 6 11:36:49 2016���������������
������������Python3 ��������������������� _thread ��� threading ���������������������������
_thread ������������������������������������������������������������������������������ threading ���������������������������������������
threading ������������������ _thread ���������������������������������������������������������
- threading.currentThread(): ������������������������������
- threading.enumerate(): ������������������������������������������list������������������������������������������������������������������������������������������
- threading.activeCount(): ���������������������������������������len(threading.enumerate())���������������������
���������������������������������������������������Thread���������������������Thread���������������������������
- run(): ������������������������������������
- start(): ���������������������
- join([time]): ������������������������
- isAlive(): ������������������������������
- getName(): ������������������
- setName(): ������������������
������ threading ��������������������������������������������� threading.Thread ������������������������������������������������������ start() ������������������������������������������������ run() ���������
���������
#!/usr/bin/python3import threadingimport timeexitFlag = 0class myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print("���������������" + self.name) print_time(self.name, self.counter, 5) print("���������������" + self.name)def print_time(threadName, delay, counter): while counter: if exitFlag: threadName.exit() time.sleep(delay) print("%s: %s" % (threadName, time.ctime(time.time()))) counter -= 1# ���������������thread1 = myThread(1, "Thread-1", 1)thread2 = myThread(2, "Thread-2", 2)# ���������������thread1.start()thread2.start()# ������������������thread1.join()thread2.join()print("���������������")
������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������0���������"set"���������������������������������1������������"print"���������������������������������������������������������������"set"���������������������������"print"���������������������������������������������0������1������������������������������������������������������������������������������������
���������������������������������������������������������������������"set"������������������������������������������������������������������������������������"print"������������������������������������"set"���������������������������������������������"print"���������������������������������������������"set"���������
���������
#!/usr/bin/python3import threadingimport timeclass myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print("���������������" + self.name) # ������������������������������ threadLock.acquire() print_time(self.name, self.counter, 3) # ��������������������������������� threadLock.release()def print_time(threadName, delay, counter): while counter: time.sleep(delay) print("%s: %s" % (threadName, time.ctime(time.time()))) counter -= 1threadLock = threading.Lock()threads = []# ���������������thread1 = myThread(1, "Thread-1", 1)thread2 = myThread(2, "Thread-2", 2)# ���������������thread1.start()thread2.start()# ���������������������������threads.append(thread1)threads.append(thread2)# ������������������������for t in threads: t.join()print("���������������")
���������������������Python ��� Queue ��������������������������������������������������������������� FIFO������������������������ Queue���LIFO������������������������ LifoQueue��������������������� PriorityQueue���
������������������������������������������������������������������������������������������������������������������������
Queue ���������������������������
发表评论
最新留言
关于作者
