from threadingimport Thread, Lock

import time

g_num =0

my_lock = Lock()

def test1():

global g_num

for iin range(1000000):

if my_lock.acquire():

g_num +=1

            my_lock.release()

print(“—test1—g_num=%d” % g_num)

def test2():

global g_num

for iin range(1000000):

if my_lock.acquire():

g_num +=1

            my_lock.release()

print(“—test2—g_num=%d” % g_num)

if name main“:

p1 = Thread(target=test1)

p1.start()

p2 = Thread(target=test2)

p2.start()

在ubuntu的pycharm里,命名了test1(),test2()函数,运行时报错:

Launching unittests with arguments python -m unittest /home/cjh/python_1/day23_lock/demo03_同步锁解决问题.py in /home/cjh/python_1/day23_lock

Ran 0 tests in 0.000s

OK

将函数名改为te1(),te2()后可以正常运行

在window中相同的代码却没有问题

test为关键字,不能出现在函数名或文件中,应避免在函数及文件命中使用

文章来源于互联网,如有雷同请联系站长删除:在ubuntu的pycharm里,命名了test1(),test2()函数,运行时报错

发表评论