zoukankan      html  css  js  c++  java
  • thread 多线程

    一、为什么重写mythread类里写了run()在main里没有调用,而调用了没有写的start()
    start()让run()在新线程里面运行。你直接调用run()就是在当前线程运行了。 
    start()调用_thread的start_new_thread去运行一个bootstrap方法,在里面做一些准备工作后会调用run() 

    1.简单的多线程

    1. from time import sleep, ctime
    2. import threading
    3. def fab(x):
    4. sleep(5)
    5. print 'fab', ctime()
    6. if x < 3:
    7. return 1
    8. return (fab(x-1)+fab(x-2))
    9. def sumx(x):
    10. sleep(5)
    11. print 'sumx', ctime()
    12. if x < 2:
    13. return 1
    14. return (x+sumx(x-1))
    15. def mulx(x):
    16. sleep(5)
    17. print 'mulx', ctime()
    18. if x < 2:
    19. return 1
    20. return (x*mulx(x-1))
    21. def main():
    22. a = threading.Thread(target=fab, args=(1,))
    23. sleep(1)
    24. b = threading.Thread(target=sumx, args=(1,))
    25. sleep(1)
    26. c = threading.Thread(target=mulx, args=(1,))
    27. a.start()
    28. b.start()
    29. c.start()
    30. print threading.activeCount()
    31. print threading.enumerate()
    32. a.join()
    33. b.join()
    34. c.join()
    35. if __name__ == '__main__':
    36. main()



    二、复杂的多线程
    1. import threading
    2. from time import sleep, ctime
    3. loops = [4, 2]
    4. class MyThread(threading.Thread):
    5. def __init__(self, func, args, name=''):
    6. threading.Thread.__init__(self)
    7. self.name = name
    8. self.func = func
    9. self.args = args
    10. def getResult(self):
    11. return self.res
    12. def run(self):
    13. print 'starting', self.name, 'at:', ctime()
    14. self.res = apply(self.func, self.args)
    15. print self.name, 'finished at:', ctime()
    16. def fib(x):
    17. sleep(0.005)
    18. if x < 2: return 1
    19. return (fib(x-2) + fib(x-1))
    20. def fac(x):
    21. sleep(0.1)
    22. if x <2: return 1
    23. return (x * fac(x-1))
    24. def sum(x):
    25. sleep(0.1)
    26. if x <2: return 1
    27. return (x + sum(x-1))
    28. funcs = [fib, fac, sum]
    29. n = 12
    30. def main():
    31. nfuncs = range(len(funcs))
    32. print '*** SINGLE THREAD'
    33. for i in nfuncs:
    34. print 'starting', funcs[i].__name__, 'at:', ctime()
    35. print funcs[i](n)
    36. print funcs[i].__name__, 'finished at:', ctime()
    37. print ' *** MULTIPLE THREADS'
    38. threads = []
    39. for i in nfuncs:
    40. t = MyThread(funcs[i], (n,), funcs[i].__name__)
    41. threads.append(t)
    42. for i in nfuncs:
    43. threads[i].start()
    44. for i in nfuncs:
    45. threads[i].join()
    46. print threads[i].getResult()
    47. print 'all DONE'
    48. if __name__ == '__main__':
    49. main()



    三.多线程实例同时下载网页,args传入参数为元组
    1. import urllib
    2. import threading
    3. url = ['http://www.baidu.com', 'http://pan.baidu.com', 'http://tieba.baidu.com']
    4. def openurl(url, x):
    5. urllib.urlretrieve(url, 'd:\'+str(x)+'.txt')
    6. def main():
    7. print 'start...........'
    8. list1 = []
    9. loops = range(len(url))
    10. for i in loops:
    11. t = threading.Thread(target=openurl, args=(url[i],i))
    12. list1.append(t)
    13. for i in loops:
    14. list1[i].start()
    15. for i in loops:
    16. list1[i].join()
    17. print 'all down'
    18. if __name__ == '__main__':
    19. main()


    四、实例化类继承类初始化,重写run()函数
    1. import urllib
    2. import threading
    3. url = ['http://www.baidu.com', 'http://pan.baidu.com', 'http://tieba.baidu.com']
    4. class downloader(threading.Thread):
    5. def __init__(self, url, x):
    6. threading.Thread.__init__(self)
    7. self.url = url
    8. self.x = x
    9. def run(self):
    10. urllib.urlretrieve(self.url, 'd:\'+str(self.x)+'.txt')
    11. for i in range(len(url)):
    12. i = downloader(url[i], i)
    13. i.start()





  • 相关阅读:
    图解排序算法(三)之堆排序
    博客园添加看板娘
    php设计模式
    PHP二维数组排序 array_multisort
    php-jwt-token
    c++实现冒泡排序
    常见的排序 转
    Mac VMware Fusion CentOS7配置静态IP
    vmwar快照和克隆区别
    springboot maven打包插件
  • 原文地址:https://www.cnblogs.com/highroom/p/a9062677dfad74b156c854a0ab70d879.html
Copyright © 2011-2022 走看看