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"

 

posted @ 2017-05-10 17:39  mrsandstorm  阅读(237)  评论(0编辑  收藏  举报