我是靠谱客的博主 长情台灯,这篇文章主要介绍Python线程的创建与常用方法(实例详解),现在分享给大家,希望可以做个参考。

本篇文章给大家带来了关于python的相关知识,其中主要介绍了关于线程的创建与常用的方法,还有一些线程演示案例,下面一起来看一下,希望对大家有帮助。

推荐学习:python视频教程

线程的创建与使用

在Python中有很多的多线程模块,其中 threading 模块就是比较常用的。下面就来看一下如何利用 threading 创建线程以及它的常用方法。

线程的创建 -threading

线程对象的常用方法

接下里看一下线程对象中都有哪些常用的方法:

线程演示案例

单线程初始案例

演示 多线程之前 先看一下下面这个案例,运行结束后看看共计耗时多久

代码示例如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# coding:utf-8import timeimport random old_lists = ['罗马假日', '怦然心动', '时空恋旅人', '天使爱美丽', '天使之城', '倒霉爱神', '爱乐之城']new_lists = []def work(): if len(old_lists) == 0: # 判断 old_list 的长度,如果为0 ,则表示 该列表的内容已经被删光了 return ''old_list' 列表内容已经全部删除' old_choice_data = random.choice(old_lists) # random 模块的 choice函数可以随机获取传入的 old_list 的元素 old_lists.remove(old_choice_data) # 当获取到这个随机元素之后,将该元素从 old_lists 中删除 new_choice_data = '%s_new' % old_choice_data # 将随机获取到的随机元素通过格式化方式重新赋值,区别于之前的元素 new_lists.append(new_choice_data) # 将格式化的新的随机元素添加至 new_lists 列表 time.sleep(1)if __name__ == '__main__': strat_time = time.time() for i in range(len(old_lists)): work() if len(old_lists) ==0: print(''old_lists' 当前为:{}'.format(None)) else: print((''old_lists' 当前为:{}'.format(old_lists))) if not len(new_lists) == 0: print((''new_lists' 当前为:{}'.format(new_lists))) else: print(''new_lists' 当前为:{}'.format(None)) end_time = time.time() print('运行结束,累计耗时:{} 秒'.format(end_time - strat_time))
登录后复制

运行结果如下:


从运行输出结果我们可以看到整个脚本运行共计耗时7秒,而且 new_lists 列表内的元素都经过格式化处理后加上了 _new ;不仅如此, 因为 random模块的choice函数 原因,new_lists 的内容顺序与 old_lists 也是不一样;每次运行顺序都会不一样,所以 old_lists 的顺序是无法得到保障的。


多线程演示案例

代码示例如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# coding:utf-8import timeimport randomimport threading old_lists = ['罗马假日', '怦然心动', '时空恋旅人', '天使爱美丽', '天使之城', '倒霉爱神', '爱乐之城']new_lists = []def work(): if len(old_lists) == 0: # 判断 old_list 的长度,如果为0 ,则表示 该列表的内容已经被删光了 return ''old_list' 列表内容已经全部删除' old_choice_data = random.choice(old_lists) # random 模块的 choice函数可以随机获取传入的 old_list 的元素 old_lists.remove(old_choice_data) # 当获取到这个随机元素之后,将该元素从 old_lists 中删除 new_choice_data = '%s_new' % old_choice_data # 将随机获取到的随机元素通过格式化方式重新赋值,区别于之前的元素 new_lists.append(new_choice_data) # 将格式化的新的随机元素添加至 new_lists 列表 time.sleep(1)if __name__ == '__main__': strat_time = time.time() print(''old_lists'初始长度为:{}'.format(len(old_lists))) # 获取 old_lists 与 new_lists 最初始的长度 print(''new_lists'初始长度为:{}'.format(len(new_lists))) thread_list = [] # 定义一个空的 thread_list 对象,用以下方添加每个线程 for i in range(len(old_lists)): thread_work = threading.Thread(target=work) # 定义一个线程实例化对象执行 work 函数,因为 work 函数没有参数所以不用传 args thread_list.append(thread_work) # 将 thread_work 添加进 thread_list thread_work.start() # 启动每一个线程 for t in thread_list: # 通过for循环将每一个线程进行阻塞 t.join() if len(old_lists) ==0: print(''old_lists' 当前为:{}'.format(None), '当前长度为:{}'.format(len(old_lists))) else: print((''old_lists' 当前为:{}'.format(old_lists))) if not len(new_lists) == 0: print(''new_lists' 当前长度为:{}'.format(len(new_lists))) print(''new_lists' 当前的值为:{}'.format(new_lists)) else: print(''new_lists' 当前为:{}'.format(None)) end_time = time.time() print('运行结束,累计耗时:{} 秒'.format(end_time - strat_time))
登录后复制

运行结果如下:


从运行的结果来看,我们初始的单线程任务耗时为 7秒,在使用多线程之后,仅耗时 1秒就完成了,大大的提高了我们的运行效率。


线程的问题

通过上面的练习,我们发现线程的使用方法几乎与进程是一模一样的。它们都可以互不干扰的执行程序,也可以使得主线程的程序不需要等待子线程的任务完成之后再去执行。只不过刚刚的演示案例中我们使用了 join() 函数进行了阻塞,这里可以吧 join() 去掉,看看执行效果。

与进程一样,线程也存在着一定的问题。

  • 线程执行的函数,也同样是无法获取返回值的。
  • 当多个线程同时修改文件一样会造成被修改文件的数据错乱的错误(因为都是并发去操作一个文件,特别是在处理交易场景的时候,需要尤为注意)。

关于这些线程中存在的问题同样是可以解决的,在下一章节的 线程池与全局锁 我们会有详细的介绍。

推荐学习:python视频教程

以上就是Python线程的创建与常用方法(实例详解)的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是长情台灯最近收集整理的关于Python线程的创建与常用方法(实例详解)的全部内容,更多相关Python线程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部