Python_多线程编程

来源:互联网 发布:打印九九乘法表java 编辑:程序博客网 时间:2024/06/08 10:08

from time import sleep,ctime
>>> def loop0():
    print( 'start loop 0 at:',ctime() )
    sleep(4)
    print( 'loop0 done at :',ctime())

    
>>> def loop1():
    print('start loop 1 at :',ctime())
    sleep(2)
    print('loop1 done at:',ctime())

    
>>> def main():
    print( 'start main at:',ctime())
    loop0()
    loop1()
    print( 'All DONE at:',ctime() )

    
>>> if __name__ == '__main__':
    main()

没有线程的代码。

('start main at:', 'Sat Nov 13 19:09:58 2010')
('start loop 0 at:', 'Sat Nov 13 19:09:58 2010')
('loop0 done at :', 'Sat Nov 13 19:10:02 2010')
('start loop 1 at :', 'Sat Nov 13 19:10:02 2010')
('loop1 done at:', 'Sat Nov 13 19:10:04 2010')
('All DONE at:', 'Sat Nov 13 19:10:04 2010')

带入了现成:

from time import sleep,ctime
>>> def loop0():
    print( 'start loop 0 at:',ctime() )
    sleep(4)
    print( 'loop0 done at :',ctime())

    
>>> def loop1():
    print('start loop 1 at :',ctime())
    sleep(2)
    print('loop1 done at:',ctime())

>>> def main():
    print( 'start main at:',ctime() )
    thread.start_new_thread( loop0, () )
    thread.start_new_thread( loop1,())
    sleep(6)
    print( 'all DONE at:',ctime() )

运行结果:

('start main at:', 'Sat Nov 13 19:14:30 2010')
('start loop 0 at:', 'Sat Nov 13 19:14:30 2010')
('start loop 1 at :', 'Sat Nov 13 19:14:30 2010')
('loop1 done at:', 'Sat Nov 13 19:14:32 2010')
('loop0 done at :', 'Sat Nov 13 19:14:34 2010')
('all DONE at:', 'Sat Nov 13 19:14:36 2010')

创建多线程的函数: thread.start_new_thread

需要的模块; import thread  

start_new_thread(...)
        start_new_thread(function, args[, kwargs])
        (start_new() is an obsolete synonym)
        
        Start a new thread and return its identifier. The thread will call the
        function with positional arguments from the tuple args and keyword arguments
        taken from the optional dictionary kwargs. The thread exits when the
        function returns; the return value is ignored. The thread will also exit
        when the function raises an unhandled exception; a stack trace will be
        printed unless the exception is SystemExit.


start_new_thread( 函数名,参数),即使不带入参数,也要带入参数表,可以使用" () "

 

阅读(409) | 评论(0) | 转发(0) |
0

上一篇:vc2005中,结构体字节序对齐问题

下一篇:Python文件拷贝-shutil

相关热门文章
  • Python 之ConfigParser
  • Python中的默认参数值
  • python将rs转换成可迭代的对象...
  • Twisted用户验证之服务端详解...
  • 一个基于multiprocessing的并...
  • python 自动化测试平台 Robot ...
  • python snmp 自动化2-在python...
  • python snmp 自动化测试1-安装...
  • 自动化测试详细测试计划 模板...
  • python snmp 自动化3-修改pyth...
  • Python 动态创建类
  • 一个基于multiprocessing的并...
  • Python 数据库管理与操作...
  • 利用Bicho抓取基于Jira的缺陷...
  • Lua中的协程即协同程序...
给主人留下些什么吧!~~
原创粉丝点击