随便写写:)
【参考】:Python内置库:threading(多线程) - 山上下了雪-bky - 博客园 (cnblogs.com)
(1)多线程实现:
python旧版本thread 新版本(2-7 3 )之后的引进了threading,且thread改为_thread
ps:多线程是指打开一个线程,运行多个线程! 【个人理解:比如打开了一个cpu,在这个cpu中跑了两个任务:
任务一:函数一(参数一)
任务二:函数一(参数二)
pps:
(2)扣得~
threading.Thread创建线程简单示例:
def test_thread(para='hi', sleep=3):
"""线程运行函数"""
time.sleep(sleep)
print(para)
def main():
# 创建线程
thread_hi = threading.Thread(target=test_thread)
thread_hello = threading.Thread(target=test_thread, args=('hello', 1))
# 启动线程
thread_hi.start()
thread_hello.start()
print('Main thread has ended!')
if __name__ == '__main__':
main()
简单来说:不同的参数用了一样的函数调用,且二者同时运行!
"""
通过继承threading.Thread的子类创建线程
"""
import time
import threading
class TestThread(threading.Thread):
def __init__(self, para='hi', sleep=3):
# 重写threading.Thread的__init__方法时,确保在所有操作之前先调用threading.Thread.__init__方法
super().__init__()
self.para = para
self.sleep = sleepdef run(self):
"""线程内容"""
time.sleep(self.sleep)
print(self.para)
def main():
# 创建线程
thread_hi = TestThread()
thread_hello = TestThread('hello', 1)
# 启动线程
thread_hi.start()
thread_hello.start()
print('Main thread has ended!')
if __name__ == '__main__':
main()
"""
使用join方法阻塞主线程
"""
import time
import threading
def test_thread(para='hi', sleep=5):
"""线程运行函数"""
time.sleep(sleep)
print(para)
def main():
# 创建线程
thread_hi = threading.Thread(target=test_thread)
thread_hello = threading.Thread(target=test_thread, args=('hello', 1))
# 启动线程
thread_hi.start()
thread_hello.start()
time.sleep(2)
print('马上执行join方法了')
# 执行join方法会阻塞调用线程(主线程),直到调用join方法的线程(thread_hi)结束
thread_hi.join()
print('线程thread_hi已结束')
# 这里不会阻塞主线程,因为运行到这里的时候,线程thread_hello已经运行结束了
thread_hello.join()
print('Main thread has ended!')# 以上代码只是为了展示join方法的效果
# 如果想要等所有线程都运行完成后再做其他操作,可以使用for循环
# for thd in (thread_hi, thread_hello):
# thd.join()
#
# print('所有线程执行结束后的其他操作')
if __name__ == '__main__':
main()
join:阻塞自身的线程------可以先run别的线程,自身线程等待!
- import threading
- import time
-
- def test():
-
- for i in range(5):
- print(threading.current_thread().name+' test ',i)
- time.sleep(0.5)
-
-
- thread = threading.Thread(target=test,name='TestThread')
- thread.start()
- thread.join()
-
- for i in range(5):
- print(threading.current_thread().name+' main ', i)
- print(thread.name+' is alive ', thread.isAlive())
- time.sleep(1)
TestThread test 0
TestThread test 1
TestThread test 2
TestThread test 3
TestThread test 4
MainThread main 0
TestThread is alive False
MainThread main 1
TestThread is alive False
MainThread main 2
TestThread is alive False
MainThread main 3
TestThread is alive False
MainThread main 4
TestThread is alive False
默认的情况是,join() 会一直等待对应线程的结束,但可以通过参数赋值,等待规定的时间就好了。
def join(self, timeout=None):
timeout 是一个浮点参数,单位是秒。
如果我们更改上面的代码。
thread.join(1.0)
TestThread test 0
TestThread test 1
MainThread main 0
TestThread is alive True
TestThread test 2
TestThread test 3
MainThread main 1
TestThread is alive True
TestThread test 4
MainThread main 2
TestThread is alive False
MainThread main 3
TestThread is alive False
MainThread main 4
TestThread is alive False主线程只等待了 1 秒钟。
参考:https://blog.csdn.net/briblue/article/details/85101144
每一个 Thread 都有一个 name 的属性,代表的就是线程的名字,这个可以在构造方法中赋值。
import threading
import timedef test():
for i in range(5):
print(threading.current_thread().name+' test ',i)
time.sleep(1)
thread = threading.Thread(target=test)
thread.start()for i in range(5):
print(threading.current_thread().name+' main ', i)
time.sleep(1)
result:Thread-1 test 0
MainThread main 0
Thread-1 test 1
MainThread main 1
Thread-1 test 2
MainThread main 2
Thread-1 test 3
MainThread main 3
Thread-1 test 4
MainThread main 4
thread = threading.Thread(target=test,name='TestThread') 定义线程名字
值得注意的是,is_alive() 返回 True 的情况是 Thread 对象被正常初始化,start() 方法被调用,然后线程的代码还在正常运行。