多线程中的局部变量

在实现多线程业务时,如果没有涉及到共享数据处理的业务,还是使用局部变量,必将,在处理共享数据时,还是需要加锁;线程与线程间的局部变量相互独立,变量的处理互补干扰。

在多线程的场景下,针对线程中的局部变量,如果需要让其他业务操作此变量时,此时

  • 局部变量相对当前线程来讲,是全局变量;

  • 局部变量相对其他线程来讲,是局部变量;

为了实现当前线程的局部变量被部分业务处理时,以两个小例子说明下

示例 1:自定义全局变量,以当前线程做为 key

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from threading import current_thread, Thread

global_variable = {}


def handler():
    std = global_variable[current_thread()]
    print 'Hello {} and this thread\'s name is {}'.format(std, current_thread().name)


def test_thread(name):
    global_variable[current_thread()] = name
    handler()


t1 = Thread(target=test_thread, args=('Kobe', ), name='thread_kobe')
t2 = Thread(target=test_thread, args=('TMC', ), name='thread_tmc')

t1.start()
t2.start()

t1.join()
t2.join()

print global_variable
print 'end......'

运行结果:

Hello Kobe and this thread's name is thread_kobe
 Hello TMC and this thread's name is thread_tmc
{<Thread(thread_kobe, stopped 123145306509312)>: u'Kobe', <Thread(thread_tmc, stopped 123145310715904)>: u'TMC'}
end......

这样,当前线程的数据不会被其他线程捕捉和处理,但是不建议这么做,毕竟 threading 模块提供了 local

示例 2:使用 local

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from threading import current_thread, Thread, local

local_variable = local()


def handler():
    std = local_variable.name
    print 'Hello {} and this thread\'s name is {}'.format(std, current_thread().name)


def test_thread(name):
    local_variable.name = name
    handler()


t1 = Thread(target=test_thread, args=('Kobe', ), name='thread_kobe')
t2 = Thread(target=test_thread, args=('TMC', ), name='thread_tmc')

t1.start()
t2.start()

t1.join()
t2.join()

print local_variable
print 'end......'

运行结果如下:

Hello Kobe and this thread's name is thread_kobe
Hello TMC and this thread's name is thread_tmc
<thread._local object at 0x10f3d36b0>
end......

参考


黑月亮
点滴记录,步步成长

现实与完美之间

1.6k 声望
24 粉丝
0 条评论
推荐阅读
centos | 修改静态 IP
设置 Centos 为使用静态 IP1 修改网络配置 {代码...} 修改后的内容如下 {代码...} 2 重启网络服务 {代码...} 3 查看地址 {代码...} 参考来源:[链接]

青阳半雪阅读 1.8k评论 3

数据结构与算法:二分查找
一、常见数据结构简单数据结构(必须理解和掌握)有序数据结构:栈、队列、链表。有序数据结构省空间(储存空间小)无序数据结构:集合、字典、散列表,无序数据结构省时间(读取时间快)复杂数据结构树、 堆图二...

白鲸鱼9阅读 5.4k

滚蛋吧,正则表达式!
你是不是也有这样的操作,比如你需要使用「电子邮箱正则表达式」,首先想到的就是直接百度上搜索一个,然后采用 CV 大法神奇地接入到你的代码中?

良许3阅读 1.6k

搭个ChatGPT算法模型,从哪开始?
最近 ChatGPT 很火,火到了各行各业。记得去年更多的还是码农最新体验后拿它搜代码,现在各行各业都进来体验,问它咋理财、怎么写报告和给小孩起名。😂 也因此让小傅哥在头条的一篇关于 ChatGPT 的文章都有了26万...

小傅哥6阅读 1.4k

封面图
程序员适合创业吗?
大家好,我是良许。从去年 12 月开始,我已经在视频号、抖音等主流视频平台上连续更新视频到现在,并得到了不错的评价。每个视频都花了很多时间精力用心制作,欢迎大家关注哦~考虑到有些小伙伴没有看过我的视频,...

良许3阅读 1.3k

PyCharm 激活破解教程, 2023 年 2 月亲测有用
本文分享一下PyCharm 2022.2.3 版本最新激活破解教程,注意不要使用太新的版本,都是 Jetbrains 产品,本文专门配上了 Pycharm 的图片,跟着下面教程一步一步来即可。

程序员徐公阅读 11.1k评论 1

Ubuntu20.04 从源代码编译安装 python3.10
Ubuntu 22.04 Release DateUbuntu 22.04 Jammy Jellyfish is scheduled for release on April 21, 2022If you’re ready to use Ubuntu 22.04 Jammy Jellyfish, you can either upgrade your current Ubuntu syste...

ponponon1阅读 4.6k评论 1

现实与完美之间

1.6k 声望
24 粉丝
宣传栏