Python编程之线程中的信息隔离!这才叫Python!

前端之家收集整理的这篇文章主要介绍了Python编程之线程中的信息隔离!这才叫Python!前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<p style="margin:10px auto;color:rgb(0,0);font-family:Verdana,Arial,Helvetica,sans-serif;font-size:14px;text-align:left;background-color:rgb(238,238,238);">大家好, <code style="margin:0px;padding:0px;">并发编程 进入第三篇。

<p style="margin:10px auto;color:rgb(0,238);">上班第一天,大家应该比较忙吧。小明也是呢,所以今天的内容也很少。只要几分钟就能学完。

<p style="margin:10px auto;color:rgb(0,238);">昨天我们说,线程与线程之间要通过消息通信来控制程序的执行。

<p style="margin:10px auto;color:rgb(0,238);">讲完了消息通信,今天就来探讨下线程里的 <code style="margin:0px;padding:0px;">信息隔离 是如何做到的。

<p style="margin:10px auto;color:rgb(0,238);">大家注意:

<p style="margin:10px auto;color:rgb(0,238);"><code style="margin:0px;padding:0px;">信息隔离 ,这并不是官方命名的名词,也不是网上广为流传的名词。是我为了方便理解而自创的,大家知道就好咯。

<p style="margin:10px auto;color:rgb(0,238);">本文目录

<ul class="list-paddingleft-2" style="margin-bottom:0px;margin-left:30px;color:rgb(0,238);"><li style="margin-top:0px;margin-bottom:1em;margin-left:0px;padding:0px;"><p style="margin:10px auto;">初步认识信息隔离

<li style="margin-top:0px;margin-bottom:1em;margin-left:0px;padding:0px;"><p style="margin:10px auto;">信息隔离的意义何在

<p style="margin:10px auto;color:rgb(0,238);">. 初步认识信息隔离

<p style="margin:10px auto;color:rgb(0,238);">什么是 <code style="margin:0px;padding:0px;">信息隔离 ?

<p style="margin:10px auto;color:rgb(0,238);">比如说,咱有两个线程,线程A里的变量,和线程B里的变量值不能共享。这就是 <code style="margin:0px;padding:0px;">信息隔离 。

<p style="margin:10px auto;color:rgb(0,238);">你可能要说,那变量名取不一样不就好啦?

<p style="margin:10px auto;color:rgb(0,238);">是的,如果所有的线程都不是由一个class实例化出来的同一个对象,确实是可以。这个问题我们暂且挂着,后面我再说明。

<p style="margin:10px auto;color:rgb(0,238);">那么,如何实现 <code style="margin:0px;padding:0px;">信息隔离 呢?

<p style="margin:10px auto;color:rgb(0,238);">在Python中,其提供了 <code style="margin:0px;padding:0px;">threading.local 这个类,可以很方便的控制变量的隔离,即使是同一个变量,在不同的线程中,其值也是不能共享的。

<p style="margin:10px auto;color:rgb(0,238);">用代码来看下

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">友情提醒:

<p style="margin:10px auto;color:rgb(0,238);">以下代码,若要理解,可能需要你了解下socket的网络编程相关内容。我在前几天的文章中有发布一篇相关的文章,没有基础的同学可以先去看看那篇文章

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">大家也可以关注一下这个程序员的博客

<p style="margin:10px auto;color:rgb(0,238);">

<p style="margin:10px auto;color:rgb(0,238);">欢迎大家加入万人交流群:125240963

猜你在找的Python相关文章