python多线程编程例子实验

实验用参考来源于网上教程:

import threading
import time
class myThread(threading. Thread):              #定义线程类
     def __init__(self,threadID,name,counter):       #构造函数初始化
         threading.Thread.__init__(self)            #thread是比较底层模块,threading是对thread做了一些包装的,可以更加方便的被使用。
         self.threadID=threadID
         self.name=name
         self.counter=counter
     def run(self):
         print " starting "+self.name
         threadLock.acquire()         #threading.Lock()的属性acquire()
         print_time(self.name,self.counter,3)
         threadLock.release()        #threading.Lock()的属性acquire()
#函数输出      
def print_time(threadName,delay,counter):
        while counter:
            time.sleep(delay)
            print "%s:%s " %(threadName,time.ctime(time.time()))
threadLock=threading.Lock()           #threading下的函数Lock()

threads=[]

thread1=myThread(1,"Thread-1",1)
thread2=myThread(2,"Thread-2",2)

thread1.start()  #thread类下的函数start方法(类属性)
thread2.start()

threads.append(thread1)
threads.append(thread2)

for t in threads:
   t.join()
print"exit"  

1、上面比较混乱的多线程例子是函数的调用。函数完全不知道来源于哪里?其中threading 包含thread,threading又含有很多函数,而thread也有很多函数。

>>> dir(thread)
['LockType', '__doc__', '__name__', '__package__', '_count', '_local', 'allocate', 'allocate_lock', 'error', 'exit', 'exit_thread', 'get_ident', 'interrupt_main', 'stack_size', 'start_new', 'start_new_thread']

thread中含有最初用函数建立线程的start_new_thread,exit等函数。

dir(threading)
['BoundedSemaphore', 'Condition', 'Event', 'Lock', 'RLock', 'Semaphore', 'Thread', 'ThreadError', 'Timer', '_BoundedSemaphore', '_Condition', '_DummyThread', '_Event', '_MainThread', '_RLock', '_Semaphore', '_Timer', '_VERBOSE', '_Verbose', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_active', '_active_limbo_lock', '_after_fork', '_allocate_lock', '_count', '_counter', '_deque', '_enumerate', '_format_exc', '_get_ident', '_limbo', '_newname', '_pickSomeNonDaemonThread', '_profile_hook', '_shutdown', '_sleep', '_start_new_thread', '_sys', '_test', '_time', '_trace_hook', 'activeCount', 'active_count', 'currentThread', 'current_thread', 'enumerate', 'local', 'setprofile', 'settrace', 'stack_size', 'warnings']

threading 模块包含Thread,currentThread,activeCount等模块。
2、用类建立多线程类,还有thread的类方法。

['_Thread__args', '_Thread__block', '_Thread__bootstrap', '_Thread__bootstrap_inner', '_Thread__daemonic', '_Thread__delete', '_Thread__exc_clear', '_Thread__exc_info', '_Thread__ident', '_Thread__initialized', '_Thread__kwargs', '_Thread__name', '_Thread__started', '_Thread__stderr', '_Thread__stop', '_Thread__stopped', '_Thread__target', '_Verbose__verbose', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_block', '_note', '_reset_internal_locks', '_set_daemon', '_set_ident', 'counter', 'daemon', 'getName', 'ident', 'isAlive', 'isDaemon', 'is_alive', 'join', 'name', 'run', 'setDaemon', 'setName', 'start', 'threadID']

常见的属性有这些’counter’, ‘daemon’, ‘getName’, ‘ident’, ‘isAlive’, ‘isDaemon’, ‘is_alive’, ‘join’, ‘name’, ‘run’, ‘setDaemon’, ‘setName’, ‘start’, ‘threadID’。

就到这吧!!菜鸟python。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值