python多线程lock只对当前进程有影响

python threading模块lock锁除了对当前进程线程有影响,是否还对其它进程线程有影响
做了个测试验证对其它进程中线程没有影响
直接上脚本
1.py

import  threading,os
import  datetime,time
lock=threading.Lock()
def fun():
    global  lock
    lock.acquire()
    time.sleep(5)
    print(f'pid:{os.getpid()}-{datetime.datetime.now()}-hello world-{threading.get_ident()}',threading.current_thread().name)
    lock.release()
t1=[]
for i in range(10):
    t1.append(threading.Thread(target=fun,name=f'脚本1线程-{i}'))
for i in t1:
    i.start()


print("脚本1主线程结束",threading.get_ident())
print(f'当前进程ID:{os.getpid()}')

2.py

import  threading,os
import  datetime,time
def fun():
    time.sleep(5)
    print(f'pid:{os.getpid()}-{datetime.datetime.now()}-hello world-{threading.get_ident()}',threading.current_thread().name)

t1=[]
for i in range(5):
    t1.append(threading.Thread(target=fun,name=f'脚本2线程-{i}'))
for i in t1:
    i.start()

print("脚本2主线程结束",threading.get_ident())
print(f'当前进程ID:{os.getpid()}')

脚本1和2现在开始同时执行
脚本1执行结果

(python38) [root@mysql01 dtask]# python 1.py 
脚本1主线程结束 139891861677888
当前进程ID:18462
pid:18462-2024-10-23 13:36:37.872807-hello world-139891728090880 脚本1线程-0
pid:18462-2024-10-23 13:36:42.876340-hello world-139891648231168 脚本1线程-1
pid:18462-2024-10-23 13:36:47.884442-hello world-139891639838464 脚本1线程-2
pid:18462-2024-10-23 13:36:52.894117-hello world-139891631445760 脚本1线程-3
pid:18462-2024-10-23 13:36:57.894989-hello world-139891623053056 脚本1线程-4
pid:18462-2024-10-23 13:37:02.903469-hello world-139891614660352 脚本1线程-5
pid:18462-2024-10-23 13:37:07.909521-hello world-139891606267648 脚本1线程-6
pid:18462-2024-10-23 13:37:12.917250-hello world-139891597874944 脚本1线程-7
pid:18462-2024-10-23 13:37:17.924179-hello world-139891111360256 脚本1线程-8
pid:18462-2024-10-23 13:37:22.933354-hello world-139891102967552 脚本1线程-9

脚本2执行结果:

(python38) [root@mysql01 dtask]# python 2.py 
脚本2主线程结束 140367038760768
当前进程ID:18475
pid:18475-2024-10-23 13:36:39.900646-hello world-140366905173760 脚本2线程-0
pid:18475-2024-10-23 13:36:39.900939-hello world-140366896781056 脚本2线程-1
pid:18475-2024-10-23 13:36:39.900982-hello world-140366888388352 脚本2线程-2
pid:18475-2024-10-23 13:36:39.901013-hello world-140366879995648 脚本2线程-3
pid:18475-2024-10-23 13:36:39.901044-hello world-140366871602944 脚本2线程-4

可以看到脚本1和脚本2是两个进程,这两个单独进程子线程和主线程的进程ID都一致
进程1的线程lock锁对进程2线程lock锁没有影响,通过执行时间可以判断,这是通过两个单独python脚本文件执行看出
那么在一个python脚本中使用多进程然后每个进程进行多线程执行最后结论一致

import  threading,os,multiprocessing
import  datetime,time

lock=threading.Lock()
def fun():
    global  lock
    lock.acquire()
    time.sleep(5)
    print(f'{datetime.datetime.now()}-执行进程pid:{os.getpid()},父进程ID:{os.getppid()},进程名:{multiprocessing.current_process().name},线程ID:-{threading.get_ident()}','线程名',threading.current_thread().name)
    lock.release()
t1=[]
def multi():
    for i in range(10):
        t1.append(threading.Thread(target=fun,name=f'线程-{i}'))
    for i in t1:
        i.start()

p1=multiprocessing.Process(target=multi,name='进程1')
p2=multiprocessing.Process(target=multi,name='进程2')

p=[]
p.append(p1)
p.append(p2)
for i in p:
    i.start()
for i in p:
    i.join()
print("脚本主线程结束线程ID:",threading.get_ident(),'脚本主进程结束进程ID:',os.getpid())

运行结果

(python38) [root@mysql01 dtask]# python 1.py 
2024-10-23 14:26:25.964214-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668388787968 线程名 线程-0
2024-10-23 14:26:25.964703-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668388787968 线程名 线程-0
2024-10-23 14:26:30.970588-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668380395264 线程名 线程-1
2024-10-23 14:26:30.970729-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668380395264 线程名 线程-1
2024-10-23 14:26:35.976089-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668299114240 线程名 线程-2
2024-10-23 14:26:35.976345-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668299114240 线程名 线程-2
2024-10-23 14:26:40.982185-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668290721536 线程名 线程-3
2024-10-23 14:26:40.982317-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668290721536 线程名 线程-3
2024-10-23 14:26:45.987790-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668282328832 线程名 线程-4
2024-10-23 14:26:45.987963-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668282328832 线程名 线程-4
2024-10-23 14:26:50.994082-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668273936128 线程名 线程-5
2024-10-23 14:26:50.994225-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668273936128 线程名 线程-5
2024-10-23 14:26:55.999937-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668265543424 线程名 线程-6
2024-10-23 14:26:56.000075-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668265543424 线程名 线程-6
2024-10-23 14:27:01.005663-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668257150720 线程名 线程-7
2024-10-23 14:27:01.005815-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668257150720 线程名 线程-7
2024-10-23 14:27:06.011907-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140668248758016 线程名 线程-8
2024-10-23 14:27:06.012053-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140668248758016 线程名 线程-8
2024-10-23 14:27:11.016922-执行进程pid:21774,父进程ID:21772,进程名:进程2,线程ID:-140667829352192 线程名 线程-9
2024-10-23 14:27:11.017219-执行进程pid:21773,父进程ID:21772,进程名:进程1,线程ID:-140667829352192 线程名 线程-9
脚本主线程结束线程ID: 140668536698688 脚本主进程结束进程ID: 21772

可以看到同一个父进程衍生出来两个紫禁城,然后线程lock锁并没有产生影响对另外一个进程线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值