zoukankan      html  css  js  c++  java
  • Python 动手——多线程1

    个人想做一个多线程的数据收发小程序,现在从多线程开始着手,一步一步完成。

    1. 多线程热热手:

    # -*- coding: utf-8 -*-
    
    import threading
    import time
    
    
    class rt_thread_create (threading.Thread):
        def __init__(self, priority, name, func):
            threading.Thread.__init__(self)
            self.priority = priority
            self.name = name
            self.func = func
    
        def run(self):
            print "rt_start " + self.name
            mutex.acquire()
            self.func(self.priority,self.name)
            mutex.release()
    
    def thread_1_entry(priority, threadName):
        time.sleep(1)
        print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
    
    def thread_2_entry(priority, threadName):
        if 1 :#选择是死循环或者是执行一次
            time.sleep(1)
            print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
        else :
            while (1):
                time.sleep(1)
                print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
    mutex = threading.Lock()
    threads = []
    
    # Create new threads
    thread1 = rt_thread_create (1, "thread1", thread_1_entry)
    thread2 = rt_thread_create (2, "thread2", thread_2_entry)
    
    # Start new Threads
    thread1.start()
    thread2.start()
    
    # Add threads to thread list
    threads.append(thread1)
    threads.append(thread2)
    
    # Wait for all threads to complete
    for t in threads:
        t.join()
    print "Exiting Main Thread"

     2. 加入串口,打印收到的数据

    # -*- coding: utf-8 -*-
    
    import threading
    import serial
    import time
    
    
    class rt_thread_create (threading.Thread):
        def __init__(self, priority, name, func):
            threading.Thread.__init__(self)
            self.priority = priority
            self.name = name
            self.func = func
    
        def run(self):
            print "rt_start " + self.name
            self.func(self.priority,self.name)
    
    
    def thread_1_entry(priority, threadName):
        time.sleep(1)
        print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
    
    def thread_2_entry(priority, threadName):
        if 1 :#选择是死循环或者是执行一次
            time.sleep(1)
            print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
        else :
            while (1):
                time.sleep(1)
                print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
    
    def thread_3_entry(priority, threadName):
        mutex.acquire()
        mutex.release()
        time.sleep(1)
    
        ser = serial.Serial('com6', 115200, timeout=0.05)
        print "ser.isOpen() = ", ser.isOpen()
        while (1):
            str = ser.read(1000)
            if len(str) > 0:
                for m in xrange(len(str)):
                    print '%02X' % ord(str[m]),
                print ""
        ser.close()
    
    
    mutex = threading.Lock()
    threads = []
    
    # Create new threads
    thread1 = rt_thread_create (1, "thread1", thread_1_entry)
    thread2 = rt_thread_create (2, "thread2", thread_2_entry)
    thread3 = rt_thread_create (3, "thread3", thread_3_entry)
    
    
    
    
    
    
    # Start new Threads
    thread1.start()
    thread2.start()
    thread3.start()
    
    # Add threads to thread list
    threads.append(thread1)
    threads.append(thread2)
    threads.append(thread3)
    
    # Wait for all threads to complete
    for t in threads:
        t.join()
    print "Exiting Main Thread"

     3. 一个线程接收,一个线程负责打印

    # -*- coding: utf-8 -*-
    
    import threading
    import serial
    import time
    
    
    class rt_thread_create (threading.Thread):
        def __init__(self, priority, name, func):
            threading.Thread.__init__(self)
            self.priority = priority
            self.name = name
            self.func = func
    
        def run(self):
            print "rt_start " + self.name
            self.func(self.priority,self.name)
    
    
    def thread_1_entry(priority, threadName):
        time.sleep(1)
        print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
    
    def thread_2_entry(priority, threadName):
        if 1 :#选择是死循环或者是执行一次
            time.sleep(1)
            print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
        else :
            while (1):
                time.sleep(1)
                print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
    
    def thread_3_entry(priority, threadName):
        time.sleep(1)
        ser = serial.Serial('com6', 115200, timeout=0.05)
        print "ser.isOpen() = ", ser.isOpen()
        while (1):
            rev = ser.read(1000)
            if len(rev) > 0:
                for m in xrange(len(rev)):
                    # print '%02X' % ord(rev[m]),
                    mutex.acquire()
                    classmate.append(rev[m])
                    mutex.release()
                print ""
        ser.close()
    
    def thread_4_entry(priority, threadName):
        time.sleep(1)
        print "%d: %s: %s" % (priority, threadName, time.ctime(time.time()))
        while (1):
            time.sleep(3)
            if len(classmate)> 0 :
                print "len(classmate) = %d"%(len(classmate))
                print classmate
                mutex.acquire()
                classmate.pop(0)
                mutex.release()
    
    classmate = []
    print len(classmate)
    mutex = threading.Lock()
    threads = []
    
    # Create new threads
    thread1 = rt_thread_create (1, "thread1", thread_1_entry)
    thread2 = rt_thread_create (2, "thread2", thread_2_entry)
    thread3 = rt_thread_create (3, "thread3", thread_3_entry)
    thread4 = rt_thread_create (4, "thread4", thread_4_entry)
    
    
    # Start new Threads
    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()
    
    # Add threads to thread list
    threads.append(thread1)
    threads.append(thread2)
    threads.append(thread3)
    threads.append(thread4)
    
    # Wait for all threads to complete
    for t in threads:
        t.join()
    print "Exiting Main Thread"
  • 相关阅读:
    VisualSVN Server 和 Subversion (都是服务器端安装)
    pl/sql导出dmp格式数据时,命令行一闪而退的问题
    Linux各种时间类型与时间函数提供技术文档
    erlang tuple的一些操作
    erlang 题目:一个integer列表,按照数字出现的次数由少到多排序,相同的数字小 的在前面
    一些erlang 题目
    各种排序
    erlang gen_tcp 详细例子
    erlang receive语句大诠释
    ets结合record的增删改查操作
  • 原文地址:https://www.cnblogs.com/mrsandstorm/p/6837187.html
Copyright © 2011-2022 走看看