Skip to main content

阻塞器, 单人阻塞器和多人阻塞器

Project description

阻塞器, 单人阻塞器和多人阻塞器


单人阻塞器:
一个线程调用lock()会立即阻塞, 直到另一个线程调用unlock()为止, 同一个阻塞器只能阻塞一个线程
测试代码:
if __name__ == '__main__':
print('测试单人阻塞器')


def fun():
for i in range(5):
time.sleep(1)
a.unlock()


import threading
import time

a = BLock()
threading.Thread(target=fun).start()

for i in range(5):
print('开始锁定', i, time.strftime('%H:%M:%S', time.localtime()))
a.lock() # 阻塞
print(' 解除', i, time.strftime('%H:%M:%S', time.localtime()))

print('结束\n\n')


多人阻塞器:
任何线程在调用lock()的时候都会被阻塞, 直到有一个线程调用一次unlock()来解除所有阻塞
测试代码
if __name__ == '__main__':
print('测试多人阻塞器')


def fun1(value):
print('阻塞', value)
a.lock()
print(' 解除', value)


def fun2():
print('--2秒后解除所有阻塞--', time.strftime('%H:%M:%S', time.localtime()))
time.sleep(2)
print('--即将解除所有阻塞--', time.strftime('%H:%M:%S', time.localtime()))
a.unlock()


import threading
import time

a = BLock_more()

for i in range(5):
threading.Thread(target=fun1, args=(i,)).start()
threading.Thread(target=fun2).start()

a.join()
print('结束')


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zblocker-1.0.2.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zblocker-1.0.2-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file zblocker-1.0.2.tar.gz.

File metadata

  • Download URL: zblocker-1.0.2.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for zblocker-1.0.2.tar.gz
Algorithm Hash digest
SHA256 238d3b64d826d749dffdc10e79b840fa62a7bbc3bd3713e0da7e61ec1e013e02
MD5 d87f3056eab4b5330df88751cd30d633
BLAKE2b-256 43dae828a62ef1c4125763365df843a853282dff5770c677696e4e7c356149f3

See more details on using hashes here.

File details

Details for the file zblocker-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: zblocker-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.5

File hashes

Hashes for zblocker-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ceebb44ab793205f97482521fb45163e0de8dc08af772ddbedecc916f0b1b69e
MD5 e88436738e46e7afa6fcdeb683abd881
BLAKE2b-256 3d620097c96e1341b7f40528b75d6282d824433e31bfe300bad5288e765822e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page