2024年Python最全Python多线程详解_python 可以使用几个线程

(
)
:

print

‘the curent threading  %s is running’

%

threading
.
current_thread
(
)
.
name

time
.
sleep
(
4
)

print

‘the curent threading  %s is ended’

%

threading
.
current_thread
(
)
.
name

print

‘the curent threading  %s is running’

%

threading
.
current_thread
(
)
.
name

t

=

threading
.
Thread
(
target

target
)

t
.
setDaemon
(
True
)

t
.
start
(
)

t
.
join
(
)

print

‘the curent threading  %s is ended’

%

threading
.
current_thread
(
)
.
name

输出如下:

the
curent
threading  
MainThread
is

running

the
curent
threading  
Thread

1

is

runningthe
curent
threading  
MainThread
is

ended

|

如果加上join,并设置等待时间,就会等待线程一段时间再退出:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

|

import
threading

import
time

def
target
(
)
:

print

‘the curent threading  %s is running’

%

threading
.
current_thread
(
)
.
name

time
.
sleep
(
4
)

print

‘the curent threading  %s is ended’

%

threading
.
current_thread
(
)
.
name

print

‘the curent threading  %s is running’

%

threading
.
current_thread
(
)
.
name

t

=

threading
.
Thread
(
target

target
)

t
.
setDaemon
(
True
)

t
.
start
(
)

t
.
join
(
1
)

输出:

the
curent
threading  
MainThread
is

running

the
curent
threading  
Thread

1

is

running

the
curent
threading  
MainThread
is

ended

主线程等待
1秒,就自动结束,并杀死子线程。如果
join不加等待时间,
t
.
join
(
)
,就会一直等待,一直到子线程结束,输出如下:

the
curent
threading  
MainThread
is

running

the
curent
threading  
Thread

1

is

running

the
curent
threading  
Thread

1

is

ended

the
curent
threading  
MainThread
is

ended

|

3、线程锁和ThreadLocal

(1)线程锁

对于多线程来说,最大的特点就是线程之间可以共享数据,那么共享数据就会出现多线程同时更改一个变量,使用同样的资源,而出现死锁、数据错乱等情况。

假设有两个全局资源,a和b,有两个线程thread1,thread2. thread1占用a,想访问b,但此时thread2占用b,想访问a,两个线程都不释放此时拥有的资源,那么就会造成死锁。

对于该问题,出现了Lock。 当访问某个资源之前,用Lock.acquire()锁住资源,访问之后,用Lock.release()释放资源。

1

2

3

4

5

6

7

8

9

10

11

12

13

|

a

=

3

lock

=

threading
.
Lock
(
)

def
target
(
)
:

print

‘the curent threading  %s is running’

%

threading
.
current_thread
(
)
.
name

time
.
sleep
(
4
)

global

a

lock
.
acquire
(
)

try
:

a

+=

3

finally
:

lock
.
release
(
)

print

‘the curent threading  %s is ended’

%

threading
.
current_thread
(
)
.
name

print

‘yes’

|

用finally的目的是防止当前线程无线占用资源。

(2)ThreadLocal

介绍完线程锁,接下来出场的是ThreadLocal。当不想将变量共享给其他线程时,可以使用局部变量,但在函数中定义局部变量会使得在函数之间传递特别麻烦。ThreadLocal是非常牛逼的东西,它解决了全局变量需要枷锁,局部变量传递麻烦的两个问题。通过在线程中定义:
local_school = threading.local()
此时这个local_school就变成了一个全局变量,但这个全局变量只在该线程中为全局变量,对于其他线程来说是局部变量,别的线程不可更改。 def process_thread(name):# 绑定ThreadLocal的student: local_school.student = name

这个student属性只有本线程可以修改,别的线程不可以。代码:

1

2

3

4

5

6

7

8

9

10

11

|

local

=

threading
.
local
(
)

def
func
(
name
)
:

print

‘current thread:%s’

%

threading
.
currentThread
(
)
.
name

local
.
name

=

name

print

“%s in %s”

%

(
local
.
name
,
threading
.
currentThread
(
)
.
name
)

t1

=

threading
.
Thread
(
target

func
,
args

(
‘haibo’
,
)
)

t2

=

threading
.
Thread
(
target

func
,
args

(
‘lina’
,
)
)

t1
.
start
(
)

t2
.
start
(
)

t1
.
join
(
)

t2
.
join
(
)

|

从代码中也可以看到,可以将ThreadLocal理解成一个dict,可以绑定不同变量。
ThreadLocal用的最多的地方就是每一个线程处理一个HTTP请求,在Flask框架中利用的就是该原理,它使用的是基于Werkzeug的LocalStack。

4、Map实现多线程:

对于多线程的使用,我们经常是用thread来创建,比较繁琐:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

|

class

MyThread
(
threading
.
Thread
)
:

def
init
(
self
)
:

threading
.
Thread
.
init
(
self
)

def
run
(
self
)
:

lock
.
acquire
(
)

print
threading
.
currentThread
(
)
.
getName
(
)

lock
.
release
(
)

def
build_worker
(
num
)
:

workers

=

[
]

for

t

in

range
(
num
)
:

work

=

MyThread
(
)

work
.
start
(
)

workers
.
append
(
work
)

return

workers

def
producer
(
)
:

threads

=

build_worker
(
4
)

for

w

in

threads
:

w
.
join
(
)

(1)Python所有方向的学习路线(新版)

这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

最近我才对这些路线做了一下新的更新,知识体系更全面了。

在这里插入图片描述

(2)Python学习视频

包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。

在这里插入图片描述

(3)100多个练手项目

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。

在这里插入图片描述

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值