from multiprocessing import Lock
lock = Lock()
with lock:
print('Lock is held')
lock.acquire()
try:
print('lock is held')
finally:
lock.release()
class 囧rz():
def __enter__(self):
print('''
____
__o_/個人//_o____
/ ̄ ̄ ̄ ̄ / ̄ ̄ |||  ̄ ̄ |〵、
/ ∧__/ ∧ ∧||| ∧ ∧| 〵\
□、 _/ ,□ (|貧乏/| (゚∀゚ |||(゚∀゚ |__〵....\___
,~ ̄ ̄ ̄ ̄__ . ̄ ̄ ̄ ̄ ̄|' ̄ ̄ ̄.⊂| ̄ ̄ ̄⊂|  ̄ □ ̄〵
_|_]|三・三三|[__|_]_ __ .......|.......................|....................| _........... [|
|=-┌―┐― =―:: / ,⌒ヽ=|==== | ===// ⌒ヽ,===|.|
〵 └―┘三|┌┐ ::_|:( ・ )|.__|_____|___/ |:( ・ )||._|/
 ̄ゞゝ_ノ ̄ ̄ ̄ ̄ゞゝ_ノ ̄ ̄ゞゝ_ノ ̄ ̄ ̄ ̄ゞゝ_ノ ̄ ̄
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
''')
def __exit__(self, exc_type, exc_val, exc_tb):
print('exit 囧rz')
a = 囧rz()
with a:
print('\nstart running inside')
print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
print('stop running inside\n')
输出结果:
____
__o_/個人//_o____
/ ̄ ̄ ̄ ̄ / ̄ ̄ |||  ̄ ̄ |〵、
/ ∧__/ ∧ ∧||| ∧ ∧| 〵\
□、 _/ ,□ (|貧乏/| (゚∀゚ |||(゚∀゚ |__〵....\___
,~ ̄ ̄ ̄ ̄__ . ̄ ̄ ̄ ̄ ̄|' ̄ ̄ ̄.⊂| ̄ ̄ ̄⊂|  ̄ □ ̄〵
_|_]|三・三三|[__|_]_ __ .......|.......................|....................| _........... [|
|=-┌―┐― =―:: / ,⌒ヽ=|==== | ===// ⌒ヽ,===|.|
〵 └―┘三|┌┐ ::_|:( ・ )|.__|_____|___/ |:( ・ )||._|/
 ̄ゞゝ_ノ ̄ ̄ ̄ ̄ゞゝ_ノ ̄ ̄ゞゝ_ノ ̄ ̄ ̄ ̄ゞゝ_ノ ̄ ̄
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
start running inside
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
stop running inside
exit 囧rz
书中例子,使用contextlib模块中的装饰器来实现,可以不写__enter__和__exit__
import logging
from contextlib import contextmanager
from multiprocessing import Lock
# lock = Lock()
# with lock:
# print('Lock is held')
# lock.acquire()
# try:
# print('lock is held')
# finally:
# lock.release()
# class 囧rz():
# def __enter__(self):
# print('''
# ____
# __o_/個人//_o____
# / ̄ ̄ ̄ ̄ / ̄ ̄ |||  ̄ ̄ |〵、
# / ∧__/ ∧ ∧||| ∧ ∧| 〵\
# □、 _/ ,□ (|貧乏/| (゚∀゚ |||(゚∀゚ |__〵....\___
# ,~ ̄ ̄ ̄ ̄__ . ̄ ̄ ̄ ̄ ̄|' ̄ ̄ ̄.⊂| ̄ ̄ ̄⊂|  ̄ □ ̄〵
# _|_]|三・三三|[__|_]_ __ .......|.......................|....................| _........... [|
# |=-┌―┐― =―:: / ,⌒ヽ=|==== | ===// ⌒ヽ,===|.|
# 〵 └―┘三|┌┐ ::_|:( ・ )|.__|_____|___/ |:( ・ )||._|/
#  ̄ゞゝ_ノ ̄ ̄ ̄ ̄ゞゝ_ノ ̄ ̄ゞゝ_ノ ̄ ̄ ̄ ̄ゞゝ_ノ ̄ ̄
#  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
# ''')
#
# def __exit__(self, exc_type, exc_val, exc_tb):
# print('exit 囧rz')
#
#
# a = 囧rz()
#
#
# with a:
# print('\nstart running inside')
# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
# print('stop running inside\n')
def my_function():
logging.debug('some debug data')
logging.error('Error log here')
logging.debug('More debug data')
# 生成器
@contextmanager
def debug_logging(level):
logger = logging.getLogger()
old_level = logger.getEffectiveLevel()
logger.setLevel(level)
try:
yield
finally:
logger.setLevel(old_level)
# my_function()
with debug_logging(logging.DEBUG):
print('Inside:')
my_function()
import time
time.sleep(3)
print('___________________________________')
print('After:')
my_function()
输出结果
DEBUG:root:some debug data
ERROR:root:Error log here
DEBUG:root:More debug data
Inside:
___________________________________
ERROR:root:Error log here
After: