import asyncio
import time
import logging
logging.basicconfig(
# 用日誌列印輸出資訊
level=logging.info,
format
="%(asctime)s %(process)d %(thread)d [*] %(message)s"
)async
defmyworker
(lock,i)
: logging.info(f"試圖獲得鎖定:"
)async
with lock:
logging.info(f"目前已鎖定:"
)await asyncio.sleep(3)
logging.info(f"解鎖臨界區:"
)async
defmain()
: lock = asyncio.lock(
)await asyncio.wait(
[myworker(lock,1)
, myworker(lock,2)
, myworker(lock,3)
])if __name__ ==
'__main__'
: loop = asyncio.get_event_loop(
)try
: loop.run_until_complete(main())
logging.info(
"所有任務完成"
)except keyboardinterrupt as e:
logging.info(asyncio.task.all_tasks())
logging.info(asyncio.gather(
*asyncio.task.all_tasks())
.cancel())
loop.stop(
) loop.run_forever(
)finally
: loop.close(
)# 在jupyter不能手動關閉
輸出:
2019-11
-1513:
32:54,
11711552
12816[*
] 試圖獲得鎖定:
22019-11
-1513:
32:54,
11711552
12816[*
] 目前已鎖定:
22019-11
-1513:
32:54,
11711552
12816[*
] 試圖獲得鎖定:
12019-11
-1513:
32:54,
11811552
12816[*
] 試圖獲得鎖定:
32019-11
-1513:
32:57,
11911552
12816[*
] 解鎖臨界區:
22019-11
-1513:
32:57,
11911552
12816[*
] 目前已鎖定:
12019-11
-1513:
33:00,
12011552
12816[*
] 解鎖臨界區:
12019-11
-1513:
33:00,
12011552
12816[*
] 目前已鎖定:
32019-11
-1513:
33:03,
12011552
12816[*
] 解鎖臨界區:
32019-11
-1513:
33:03,
12011552
12816[*
] 所有任務完成
乙個游標簡單示例
下面是乙個游標的例項,方便初學者學習,也可以防止自己忘記 if exists select 1 from sysobjects where name hehe drop procedure hehe goset ansi nulls on set quoted identifier on gocre...
乙個簡單的學習示例
bob bob smith 42,30000,software sue sue jones 45,40000,hardware print bob 0 sue 2 print n print bob 0 split 1 sue 2 1.25 print n print sue people bob,...
乙個簡單的Quartz示例
1 在你使用排程器 scheduler 之前,需要先對其進行初始化。你可以使用乙個排程器工廠 schedule ctory 來完成這項任務。一些quartz的使用者可能會在jndi中儲存乙個工廠 factory 的例項,但是其他使用者可能會直接使用乙個工廠例項 就像下面的示例一樣 並發現初始化乙個排...