Python3 多线程
发布日期:2021-05-10 07:21:18 浏览次数:13 分类:精选文章

本文共 7277 字,大约阅读时间需要 24 分钟。

������������������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������������������������CPU������������������������������������������������������������������������������������������CPU���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������������������������������������������������������������������������������������������������������-- ���������������������������

    ���������������������������

  • ������������������������������������������������������
  • ���������������������������������������������������������������������������
  • Python3������������������������������������

  • _thread
  • threading(������������)
  • 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 ���������������������������

    上一篇:Python3 SMTP发送邮件
    下一篇:Python3 XML 解析

    发表评论

    最新留言

    很好
    [***.229.124.182]2025年04月06日 02时42分30秒