{"id":1579,"date":"2023-03-25T11:33:17","date_gmt":"2023-03-25T03:33:17","guid":{"rendered":""},"modified":"2023-03-25T11:33:17","modified_gmt":"2023-03-25T03:33:17","slug":"Python\u7ebf\u7a0b\u901a\u4fe1","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1579.html","title":{"rendered":"Python\u7ebf\u7a0b\u901a\u4fe1"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python\u7ebf\u7a0b\u901a\u4fe1<\/h1>\n

Python\u7ebf\u7a0b\u901a\u4fe1\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u5982\u679c\u4e00\u7fa4\u4eba\u5728\u5171\u540c\u5b8c\u6210\u4e00\u9879\u4efb\u52a1\uff0c\u90a3\u4e48\u4ed6\u4eec\u4e4b\u95f4\u5e94\u8be5\u8fdb\u884c\u6c9f\u901a\u4ee5\u6b63\u786e\u5b8c\u6210\u4efb\u52a1\u3002\u540c\u6837\u7684\u7c7b\u6bd4\u4e5f\u9002\u7528\u4e8e\u7ebf\u7a0b\u3002\u5728\u7f16\u7a0b\u4e2d\uff0c\u4e3a\u4e86\u51cf\u5c11\u5904\u7406\u5668\u7684\u7406\u60f3\u65f6\u95f4\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u591a\u4e2a\u7ebf\u7a0b\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u5206\u914d\u4e86\u4e0d\u540c\u7684\u5b50\u4efb\u52a1\u3002\u56e0\u6b64\uff0c\u5fc5\u987b\u6709\u4e00\u4e2a\u901a\u8baf\u5de5\u5177\uff0c\u5e76\u4e14\u4ed6\u4eec\u5e94\u8be5\u5f7c\u6b64\u4ea4\u4e92\u4ee5\u540c\u6b65\u65b9\u5f0f\u5b8c\u6210\u5de5\u4f5c\u3002\n <\/div>\n
\n \u8bf7\u8003\u8651\u4ee5\u4e0b\u4e0e\u7ebf\u7a0b\u4e92\u901a\u6709\u5173\u7684\u8981\u70b9-\n <\/div>\n

\u6ca1\u6709\u6027\u80fd\u63d0\u5347<\/b>-\u5982\u679c\u6211\u4eec\u65e0\u6cd5\u5728\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u4e4b\u95f4\u5b9e\u73b0\u6b63\u786e\u7684\u901a\u4fe1\uff0c\u90a3\u4e48\u5e76\u53d1\u548c\u5e76\u884c\u6027\u5e26\u6765\u7684\u6027\u80fd\u63d0\u5347\u5c31\u6ca1\u6709\u7528\u4e86\u3002<\/span>
\n \u6b63\u786e\u5b8c\u6210\u4efb\u52a1<\/b>-\u7ebf\u7a0b\u4e4b\u95f4\u6ca1\u6709\u9002\u5f53\u7684\u4e92\u901a\u673a\u5236\uff0c\u5206\u914d\u7684\u4efb\u52a1\u5c06\u65e0\u6cd5\u6b63\u786e\u5b8c\u6210\u3002<\/span>
\n \u6bd4\u8fdb\u7a0b\u95f4\u901a\u4fe1\u66f4\u6709\u6548<\/b>-\u7ebf\u7a0b\u95f4\u901a\u4fe1\u6bd4\u8fdb\u7a0b\u95f4\u901a\u4fe1\u66f4\u9ad8\u6548\uff0c\u66f4\u6613\u4e8e\u4f7f\u7528\uff0c\u56e0\u4e3a\u8fdb\u7a0b\u5185\u7684\u6240\u6709\u7ebf\u7a0b\u5171\u4eab\u76f8\u540c\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u800c\u4e0d\u5fc5\u4f7f\u7528\u5171\u4eab\u7684\u8bb0\u5fc6\u3002<\/span> <\/p>\n

\u7528\u4e8e\u7ebf\u7a0b\u5b89\u5168\u901a\u4fe1\u7684Python\u6570\u636e\u7ed3\u6784<\/h2>\n
\n \u591a\u7ebf\u7a0b\u4ee3\u7801\u5b58\u5728\u5c06\u4fe1\u606f\u4ece\u4e00\u4e2a\u7ebf\u7a0b\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u7684\u95ee\u9898\u3002\u6807\u51c6\u7684\u901a\u4fe1\u539f\u8bed\u4e0d\u80fd\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u81ea\u5df1\u7684\u590d\u5408\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u5728\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u5bf9\u8c61\uff0c\u4ee5\u4f7f\u901a\u4fe1\u7ebf\u7a0b\u5b89\u5168\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6570\u636e\u7ed3\u6784\uff0c\u5728\u5bf9\u5b83\u4eec\u8fdb\u884c\u4e86\u4e00\u4e9b\u66f4\u6539\u4e4b\u540e\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u5b89\u5168\u7684\u901a\u4fe1-\n <\/div>\n

\u96c6\u5408<\/h3>\n
\n \u4e3a\u4e86\u4ee5\u7ebf\u7a0b\u5b89\u5168\u7684\u65b9\u5f0f\u4f7f\u7528set\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u9700\u8981\u6269\u5c55set\u7c7b\u4ee5\u5b9e\u73b0\u6211\u4eec\u81ea\u5df1\u7684\u9501\u5b9a\u673a\u5236\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u8fd9\u662f\u6269\u5c55\u7c7b\u7684Python\u793a\u4f8b-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
class <\/span>extend_class(set):
   def <\/span>__init__(self, *args, **kwargs):
      self._lock = Lock<\/span>()
      super(extend_class, self).__init__<\/span>(*args, **kwargs)
   def <\/span>add(self, elem):
      self._lock.acquire<\/span>()
try:
      super(extend_class, self).add<\/span>(elem)
      finally:
      self._lock.release<\/span>()
   def <\/span>delete(self, elem):
      self._lock.acquire<\/span>()
      try:
      super(extend_class, self).delete<\/span>(elem)
      finally:
      self._lock.release<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u540d\u4e3a
\n extend_class <\/b>\u7684\u7c7b\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u8fdb\u4e00\u6b65\u4ecePython
\n set\u7c7b<\/b>\u7ee7\u627f\u3002\u5728\u6b64\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u521b\u5efa\u4e00\u4e2a\u9501\u5bf9\u8c61\u3002\u73b0\u5728\uff0c\u6709\u4e24\u4e2a\u51fd\u6570-
\n add()<\/b>\u548c
\n delete()<\/b>\u3002\u8fd9\u4e9b\u51fd\u6570\u5df2\u5b9a\u4e49\u5e76\u4e14\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5b83\u4eec\u90fd\u4f9d\u8d56
\n super <\/b>\u7c7b\u529f\u80fd\uff0c\u4f46\u6709\u4e00\u4e2a\u5173\u952e\u7684\u4f8b\u5916\u3002\n <\/div>\n

\u88c5\u9970\u5668<\/h3>\n
\n \u8fd9\u662f\u7ebf\u7a0b\u5b89\u5168\u901a\u4fe1\u7684\u53e6\u4e00\u79cd\u5173\u952e\u65b9\u6cd5\u662f\u4f7f\u7528\u88c5\u9970\u5668\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u8003\u8651\u4e00\u4e2aPython\u793a\u4f8b\uff0c\u8be5\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u88c5\u9970\u5668\uff06mminus;\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
def <\/span>lock_decorator(method):
   def <\/span>new_deco_method(self, *args, **kwargs):
      with <\/span>self._lock:
         return <\/span>method(self, *args, **kwargs)
return <\/span>new_deco_method
class <\/span>Decorator_class(set):
   def <\/span>__init__(self, *args, **kwargs):
      self._lock = Lock<\/span>()
      super(Decorator_class, self).__init__<\/span>(*args, **kwargs)
   @lock_decorator
   def <\/span>add(self, *args, **kwargs):
      return <\/span>super(Decorator_class, self).add<\/span>(elem)
   @lock_decorator
   def <\/span>delete(self, *args, **kwargs):
      return <\/span>super(Decorator_class, self).delete<\/span>(elem)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3alock_decorator\u7684\u88c5\u9970\u5668\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fdb\u4e00\u6b65\u4ecePython\u65b9\u6cd5\u7c7b\u7ee7\u627f\u3002\u7136\u540e\uff0c\u5728\u6b64\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u521b\u5efa\u4e00\u4e2a\u9501\u5bf9\u8c61\u3002\u73b0\u5728\uff0c\u6709\u4e24\u4e2a\u529f\u80fd-add()\u548cdelete()\u3002\u8fd9\u4e9b\u51fd\u6570\u5df2\u5b9a\u4e49\u5e76\u4e14\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5b83\u4eec\u90fd\u4f9d\u8d56\u4e8e\u8d85\u7c7b\u529f\u80fd\uff0c\u4f46\u6709\u4e00\u4e2a\u5173\u952e\u7684\u4f8b\u5916\u3002\n <\/div>\n

\u5217\u8868<\/h3>\n
\n \u5217\u8868\u6570\u636e\u7ed3\u6784\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5feb\u901f\u4e14\u6613\u4e8e\u5b58\u50a8\u7684\u4e34\u65f6\u5185\u5b58\u7ed3\u6784\u3002\u5728Cpython\u4e2d\uff0cGIL\u9632\u6b62\u5e76\u53d1\u8bbf\u95ee\u5b83\u4eec\u3002\u6211\u4eec\u77e5\u9053\u5217\u8868\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u662f\u5176\u4e2d\u7684\u6570\u636e\u53c8\u5982\u4f55\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u5217\u8868\u7684\u6570\u636e\u4e0d\u53d7\u4fdd\u62a4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8bd5\u56fe\u505a\u540c\u6837\u7684\u4e8b\u60c5\uff0c
\n L.append\uff08x\uff09<\/b>\u4e0d\u80fd\u4fdd\u8bc1\u8fd4\u56de\u9884\u671f\u7684\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a\uff0c\u5c3d\u7ba1
\n append()<\/b>\u662f\u539f\u5b50\u64cd\u4f5c\u4e14\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5728\u5c1d\u8bd5\u4ee5\u5e76\u53d1\u65b9\u5f0f\u4fee\u6539\u5217\u8868\u7684\u6570\u636e\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7ade\u4e89\u6761\u4ef6\u5bf9\u8f93\u51fa\u7684\u526f\u4f5c\u7528\n <\/div>\n
\n \u8981\u89e3\u51b3\u6b64\u7c7b\u95ee\u9898\u5e76\u5b89\u5168\u5730\u4fee\u6539\u6570\u636e\uff0c\u6211\u4eec\u5fc5\u987b\u5b9e\u73b0\u9002\u5f53\u7684\u9501\u5b9a\u673a\u5236\uff0c\u8be5\u673a\u5236\u8fdb\u4e00\u6b65\u786e\u4fdd\u4e86\u591a\u4e2a\u7ebf\u7a0b\u4e0d\u4f1a\u6f5c\u5728\u5730\u9677\u5165\u7ade\u4e89\u72b6\u6001\u3002\u4e3a\u4e86\u5b9e\u73b0\u9002\u5f53\u7684\u9501\u5b9a\u673a\u5236\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u524d\u9762\u7684\u793a\u4f8b\u4e2d\u90a3\u6837\u6269\u5c55\u7c7b\u3002\n <\/div>\n
\n \u5217\u8868\u4e0a\u7684\u5176\u4ed6\u4e00\u4e9b\u539f\u5b50\u64cd\u4f5c\u5982\u4e0b-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
L.append<\/span>(x)
L1.extend<\/span>(L2)
x = L[i]
x = L.pop<\/span>()
L1[i:j] = L2
L.sort<\/span>()
x = y
x.field = y
D[x] = y
D1.update<\/span>(D2)
D.keys<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd9\u91cc-\n <\/div>\n

L\uff0cL1\uff0cL2\u90fd\u662f\u5217\u8868<\/span>
\n D\uff0cD1\uff0cD2\u662f\u5b57\u5178<\/span>
\n x\uff0cy\u662f\u5bf9\u8c61<\/span>
\n i\uff0cj\u662f\u6574\u6570<\/span> <\/p>\n

\u961f\u5217<\/h3>\n
\n \u5982\u679c\u5217\u8868\u7684\u6570\u636e\u672a\u53d7\u5230\u4fdd\u62a4\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u5f97\u4e0d\u9762\u5bf9\u540e\u679c\u3002\u6211\u4eec\u53ef\u80fd\u4f1a\u83b7\u5f97\u6216\u5220\u9664\u6709\u5173\u6bd4\u8d5b\u6761\u4ef6\u7684\u9519\u8bef\u6570\u636e\u9879\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u4f7f\u7528\u961f\u5217\u6570\u636e\u7ed3\u6784\u3002\u961f\u5217\u7684\u771f\u5b9e\u793a\u4f8b\u53ef\u4ee5\u662f\u5355\u8f66\u9053\u5355\u5411\u9053\u8def\uff0c\u8f66\u8f86\u9996\u5148\u8fdb\u5165\uff0c\u7136\u540e\u9996\u5148\u79bb\u5f00\u3002\u5728\u552e\u7968\u7a97\u53e3\u548c\u516c\u4ea4\u8f66\u7ad9\u7684\u961f\u5217\u4e2d\u53ef\u4ee5\u770b\u5230\u66f4\u591a\u771f\u5b9e\u7684\u4f8b\u5b50\u3002\n <\/div>\n

Python\u7ebf\u7a0b\u901a\u4fe1_https:\/\/bianchenghao6.com_\u3010Python\u591a\u7ebf\u7a0b\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u961f\u5217\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u4e0d\u5fc5\u62c5\u5fc3\u5b9e\u73b0\u590d\u6742\u7684\u9501\u5b9a\u673a\u5236\u3002 Python\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86
\n <\u961f\u5217>
\n \u6a21\u5757\u4ee5\u5728\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u961f\u5217\u3002
\n
\n <\/\u961f\u5217>\n <\/div>\n

\u961f\u5217\u7c7b\u578b<\/h2>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u4e0d\u540c\u7c7b\u578b\u7684\u961f\u5217\u3002 Python\u5728
\n
\n
\n <\/queue><\/b>\u6a21\u5757\u4e2d\u63d0\u4f9b\u4e86\u4e09\u79cd\u53ef\u4f9b\u4f7f\u7528\u7684\u961f\u5217\u9009\u9879-\n <\/div>\n

\u666e\u901a\u961f\u5217\uff08\u5148\u8fdb\u5148\u51fa\uff0c\u5148\u8fdb\u5148\u51fa\uff09<\/span>
\n \u540e\u8fdb\u5148\u51fa\uff0c\u540e\u8fdb\u5148\u51fa<\/span>
\n \u4f18\u5148<\/span> <\/p>\n

\n \u6211\u4eec\u5c06\u5728\u540e\u7eed\u90e8\u5206\u4e2d\u4e86\u89e3\u4e0d\u540c\u7684\u961f\u5217\u3002\n <\/div>\n

\u666e\u901a\u961f\u5217\uff08\u5148\u8fdb\u5148\u51fa\uff0c\u5148\u8fdb\u5148\u51fa\uff09<\/h2>\n
\n \u5b83\u662fPython\u63d0\u4f9b\u7684\u6700\u5e38\u7528\u7684\u961f\u5217\u5b9e\u73b0\u3002\u5728\u8fd9\u79cd\u6392\u961f\u673a\u5236\u4e2d\uff0c\u65e0\u8bba\u8c01\u5148\u5230\uff0c\u90fd\u5c06\u9996\u5148\u83b7\u5f97\u670d\u52a1\u3002 FIFO\u4e5f\u79f0\u4e3a\u666e\u901a\u961f\u5217\u3002 FIFO\u961f\u5217\u53ef\u4ee5\u8868\u793a\u5982\u4e0b-\n <\/div>\n

Python\u7ebf\u7a0b\u901a\u4fe1_https:\/\/bianchenghao6.com_\u3010Python\u591a\u7ebf\u7a0b\u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

FIFO\u961f\u5217\u7684Python\u5b9e\u73b0<\/h3>\n
\n \u5728python\u4e2d\uff0cFIFO\u961f\u5217\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u5355\u7ebf\u7a0b\u548c\u591a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u3002\n <\/div>\n

\u5177\u6709\u5355\u7ebf\u7a0b\u7684FIFO\u961f\u5217<\/h3>\n
\n \u8981\u4f7f\u7528\u5355\u7ebf\u7a0b\u5b9e\u73b0FIFO\u961f\u5217\uff0c
\n Queue <\/b>\u7c7b\u5c06\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u5148\u8fdb\u5148\u51fa\u5bb9\u5668\u3002\u5143\u7d20\u5c06\u4f7f\u7528
\n put()<\/b>\u6dfb\u52a0\u5230\u5e8f\u5217\u7684\u4e00\u4e2a\"\u672b\u7aef\"\uff0c\u5e76\u4f7f\u7528
\n get()<\/b>\u4ece\u53e6\u4e00\u672b\u7aef\u9664\u53bb\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0b\u662f\u7528\u4e8e\u5b9e\u73b0\u5355\u7ebf\u7a0bFIFO\u961f\u5217\u7684Python\u7a0b\u5e8f-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>queue
q = queue.Queue<\/span>()
for <\/span><\/span>i in <\/span>range(8):
   q.put<\/span>(\"item-\"<\/span> + str(i))
while <\/span>not <\/span>q.empty<\/span>():
   print <\/span>(q.get<\/span>(), end = \" \"<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
item-0 item-1 item-2 item-3 item-4 item-5 item-6 item-7
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa\u7ed3\u679c\u8868\u660e\uff0c\u4e0a\u9762\u7684\u7a0b\u5e8f\u4f7f\u7528\u4e00\u4e2a\u7ebf\u7a0b\u6765\u8bf4\u660e\u5143\u7d20\u6309\u7167\u63d2\u5165\u987a\u5e8f\u4ece\u961f\u5217\u4e2d\u5220\u9664\u3002\n <\/div>\n

\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684FIFO\u961f\u5217<\/h3>\n
\n \u8981\u5b9e\u73b0\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684FIFO\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49myqueue()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u662f\u4ece\u961f\u5217\u6a21\u5757\u6269\u5c55\u7684\u3002\u4f7f\u7528\u5355\u7ebf\u7a0b\u5b9e\u73b0FIFO\u961f\u5217\u65f6\uff0cget()\u548cput()\u65b9\u6cd5\u7684\u5de5\u4f5c\u4e0e\u4e0a\u9762\u8ba8\u8bba\u7684\u76f8\u540c\u3002\u7136\u540e\u8981\u4f7f\u5176\u6210\u4e3a\u591a\u7ebf\u7a0b\uff0c\u6211\u4eec\u9700\u8981\u58f0\u660e\u5e76\u5b9e\u4f8b\u5316\u7ebf\u7a0b\u3002\u8fd9\u4e9b\u7ebf\u7a0b\u5c06\u4ee5FIFO\u65b9\u5f0f\u6d88\u8017\u961f\u5217\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0b\u662f\u7528\u4e8e\u5b9e\u73b0\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684FIFO\u961f\u5217\u7684Python\u7a0b\u5e8f\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>threading
import <\/span>queue
import <\/span>random
import <\/span>time
def <\/span>myqueue(queue):
   while <\/span>not <\/span>queue.empty<\/span>():
   item = queue.get<\/span>()
   if <\/span>item is <\/span>None:
   break<\/span>
   print(\"{} removed {} from <\/span>the queue\"<\/span>.format<\/span>(threading.current_thread<\/span>(), item))
   queue.task_done<\/span>()
   time.sleep<\/span>(2)
q = queue.Queue<\/span>()
for <\/span><\/span>i in <\/span>range(5):
   q.put<\/span>(i)
threads = []
for <\/span><\/span>i in <\/span>range(4):
   thread = threading.Thread<\/span>(target=myqueue, args=(q,))
   thread.start<\/span>()
   threads.append<\/span>(thread)
for <\/span><\/span>thread in <\/span>threads:
   thread.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 <Thread(Thread-3654, started 5044)> removed 0 from <\/span>the queue
<Thread(Thread-3655, started 3144)> removed 1 from <\/span>the queue
<Thread(Thread-3656, started 6996)> removed 2 from <\/span>the queue
<Thread(Thread-3657, started 2672)> removed 3 from <\/span>the queue
<Thread(Thread-3654, started 5044)> removed 4 from <\/span>the queue
<\/span><\/code><\/pre>\n<\/p><\/div>\n

LIFO\uff0c\u540e\u8fdb\u5148\u51fa\u961f\u5217<\/h2>\n
\n \u6b64\u961f\u5217\u4f7f\u7528\u4e0eFIFO\uff08\u5148\u8fdb\u5148\u51fa\uff09\u961f\u5217\u5b8c\u5168\u76f8\u53cd\u7684\u7c7b\u6bd4\u3002\u5728\u8fd9\u79cd\u6392\u961f\u673a\u5236\u4e2d\uff0c\u6700\u540e\u4e00\u4f4d\u5c06\u9996\u5148\u83b7\u5f97\u670d\u52a1\u3002\u8fd9\u7c7b\u4f3c\u4e8e\u5b9e\u73b0\u5806\u6808\u6570\u636e\u7ed3\u6784\u3002\u5b9e\u8df5\u8bc1\u660e\uff0cLIFO\u961f\u5217\u5728\u5b9e\u73b0\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08\u5982\u4eba\u5de5\u667a\u80fd\u7b97\u6cd5\uff09\u65f6\u975e\u5e38\u6709\u7528\u3002\n <\/div>\n

LIFO\u961f\u5217\u7684Python\u5b9e\u73b0<\/h3>\n
\n \u5728python\u4e2d\uff0cLIFO\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u5355\u7ebf\u7a0b\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u3002\n <\/div>\n

\u5355\u7ebf\u7a0bLIFO\u961f\u5217<\/h3>\n
\n \u8981\u4f7f\u7528\u5355\u7ebf\u7a0b\u5b9e\u73b0LIFO\u961f\u5217\uff0c
\n Queue <\/b>\u7c7b\u5c06\u4f7f\u7528\u7ed3\u6784
\n Queue <\/b> .LifoQueue\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u540e\u8fdb\u5148\u51fa\u5bb9\u5668\u3002\u73b0\u5728\uff0c\u5728\u8c03\u7528
\n put()<\/b>\u65f6\uff0c\u4e5f\u53ef\u200b\u200b\u4ee5\u4f7f\u7528
\n get()<\/b>\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u5bb9\u5668\u7684\u5934\u90e8\uff0c\u5e76\u4ece\u5934\u90e8\u79fb\u9664\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0b\u662f\u7528\u4e8e\u4f7f\u7528\u5355\u7ebf\u7a0b\u5b9e\u73b0LIFO\u961f\u5217\u7684Python\u7a0b\u5e8f-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>queue
q = queue.LifoQueue<\/span>()
for <\/span><\/span>i in <\/span>range(8):
   q.put<\/span>(\"item-\"<\/span> + str(i))
while <\/span>not <\/span>q.empty<\/span>():
   print <\/span>(q.get<\/span>(), end=\" \"<\/span>)
Output:
item-7 item-6 item-5 item-4 item-3 item-2 item-1 item-0
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa\u663e\u793a\uff0c\u4ee5\u4e0a\u7a0b\u5e8f\u4f7f\u7528\u5355\u4e2a\u7ebf\u7a0b\u6765\u8bf4\u660e\u4ee5\u4e0e\u63d2\u5165\u5143\u7d20\u76f8\u53cd\u7684\u987a\u5e8f\u4ece\u961f\u5217\u4e2d\u5220\u9664\u5143\u7d20\u3002\n <\/div>\n

\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684LIFO\u961f\u5217<\/h3>\n
\n \u5b9e\u73b0\u4e0e\u6211\u4eec\u5b8c\u6210\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684FIFO\u961f\u5217\u7684\u5b9e\u73b0\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u662f\u6211\u4eec\u9700\u8981\u4f7f\u7528
\n Queue <\/b>\u7c7b\uff0c\u8be5\u7c7b\u5c06\u901a\u8fc7\u4f7f\u7528
\n Queue.LifoQueue <\/b>\u7ed3\u6784\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u540e\u8fdb\u5148\u51fa\u5bb9\u5668\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0b\u662f\u7528\u4e8e\u5b9e\u73b0\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684LIFO\u961f\u5217\u7684Python\u7a0b\u5e8f-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>threading
import <\/span>queue
import <\/span>random
import <\/span>time
def <\/span>myqueue(queue):
   while <\/span>not <\/span>queue.empty<\/span>():
      item = queue.get<\/span>()
      if <\/span>item is <\/span>None:
      break<\/span>
print(\"{} removed {} from <\/span>the queue\"<\/span>.format<\/span>(threading.current_thread<\/span>(), item))
      queue.task_done<\/span>()
      time.sleep<\/span>(2)
q = queue.LifoQueue<\/span>()
for <\/span><\/span>i in <\/span>range(5):
   q.put<\/span>(i)
threads = []
for <\/span><\/span>i in <\/span>range(4):
   thread = threading.Thread<\/span>(target=myqueue, args=(q,))
   thread.start<\/span>()
   threads.append<\/span>(thread)
for <\/span><\/span>thread in <\/span>threads:
   thread.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 <Thread(Thread-3882, started 4928)> removed 4 from <\/span>the queue
<Thread(Thread-3883, started 4364)> removed 3 from <\/span>the queue
<Thread(Thread-3884, started 6908)> removed 2 from <\/span>the queue
<Thread(Thread-3885, started 3584)> removed 1 from <\/span>the queue
<Thread(Thread-3882, started 4928)> removed 0 from <\/span>the queue
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u4f18\u5148\u7ea7\u961f\u5217<\/h3>\n
\n \u5728FIFO\u548cLIFO\u961f\u5217\u4e2d\uff0c\u9879\u7684\u987a\u5e8f\u4e0e\u63d2\u5165\u987a\u5e8f\u6709\u5173\u3002\u4f46\u662f\uff0c\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u4f18\u5148\u7ea7\u6bd4\u63d2\u5165\u987a\u5e8f\u66f4\u91cd\u8981\u3002\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u771f\u5b9e\u7684\u4f8b\u5b50\u3002\u5047\u8bbe\u673a\u573a\u7684\u4fdd\u5b89\u4eba\u5458\u6b63\u5728\u68c0\u67e5\u4e0d\u540c\u7c7b\u522b\u7684\u4eba\u5458\u3002 VVIP\u7684\u4eba\u5458\uff0c\u822a\u7a7a\u516c\u53f8\u4eba\u5458\uff0c\u6d77\u5173\u5b98\u5458\uff0c\u7c7b\u522b\u53ef\u80fd\u4f1a\u88ab\u4f18\u5148\u68c0\u67e5\uff0c\u800c\u4e0d\u662f\u50cf\u5e73\u6c11\u767e\u59d3\u4e00\u6837\u6839\u636e\u5230\u8fbe\u7684\u65f6\u95f4\u8fdb\u884c\u68c0\u67e5\u3002\n <\/div>\n
\n \u4f18\u5148\u7ea7\u961f\u5217\u9700\u8981\u8003\u8651\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u662f\u5982\u4f55\u5f00\u53d1\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u662f\u5728\u961f\u5217\u4e2d\u6309\u4f18\u5148\u7ea7\u670d\u52a1\u4e8e\u6700\u591a\u7684\u4ee3\u7406\u4efb\u52a1\u3002\u6b64\u6570\u636e\u7ed3\u6784\u53ef\u7528\u4e8e\u6839\u636e\u4f18\u5148\u7ea7\u503c\u4ece\u961f\u5217\u4e2d\u63d0\u53d6\u9879\u76ee\u3002\n <\/div>\n

\u4f18\u5148\u7ea7\u961f\u5217\u7684Python\u5b9e\u73b0<\/h3>\n
\n \u5728python\u4e2d\uff0c\u4f18\u5148\u7ea7\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u5355\u7ebf\u7a0b\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u3002\n <\/div>\n

\u5177\u6709\u5355\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u961f\u5217<\/h3>\n
\n \u5bf9\u4e8e\u4f7f\u7528\u5355\u7ebf\u7a0b\u5b9e\u73b0\u4f18\u5148\u7ea7\u961f\u5217\uff0c
\n Queue <\/b>\u7c7b\u5c06\u4f7f\u7528\u7ed3\u6784
\n Queue <\/b> .PriorityQueue\u5728\u4f18\u5148\u7ea7\u5bb9\u5668\u4e0a\u5b9e\u73b0\u4efb\u52a1\u3002\u73b0\u5728\uff0c\u5728\u8c03\u7528
\n put()<\/b>\u65f6\uff0c\u5c06\u4e3a\u5143\u7d20\u6dfb\u52a0\u4e00\u4e2a\u503c\uff0c\u5176\u4e2d\u6700\u4f4e\u503c\u5177\u6709\u6700\u9ad8\u4f18\u5148\u7ea7\uff0c\u56e0\u6b64\u9996\u5148\u4f7f\u7528
\n get()<\/b>\u8fdb\u884c\u68c0\u7d22\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u8003\u8651\u4ee5\u4e0bPython\u7a0b\u5e8f\uff0c\u4ee5\u5b9e\u73b0\u5177\u6709\u5355\u7ebf\u7a0b\u7684Priority\u961f\u5217-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>queue as <\/span>Q
p_queue = Q.PriorityQueue<\/span>()
p_queue.put<\/span>((2, 'Urgent'<\/span>))
p_queue.put<\/span>((1, 'Most Urgent'<\/span>))
p_queue.put<\/span>((10, 'Nothing important'<\/span>))
prio_queue.put<\/span>((5, 'Important'<\/span>))
while <\/span>not <\/span>p_queue.empty<\/span>():
   item = p_queue.get<\/span>()
   print('%s - %s'<\/span> % item)
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 1 \u2013 Most Urgent
2 - Urgent
5 - Important
10 \u2013 Nothing important
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u8f93\u51fa\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u961f\u5217\u6839\u636e\u4f18\u5148\u7ea7\u5b58\u50a8\u4e86\u9879\u76ee-\u503c\u8d8a\u5c0f\u4f18\u5148\u7ea7\u8d8a\u9ad8\u3002\n <\/div>\n

\u5177\u6709\u591a\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u961f\u5217<\/h3>\n
\n \u8be5\u5b9e\u73b0\u7c7b\u4f3c\u4e8e\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684FIFO\u548cLIFO\u961f\u5217\u7684\u5b9e\u73b0\u3002\u552f\u4e00\u7684\u533a\u522b\u662f\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528
\n Queue <\/b>\u7c7b\u901a\u8fc7\u4f7f\u7528
\n Queue.PriorityQueue <\/b>\u7ed3\u6784\u6765\u521d\u59cb\u5316\u4f18\u5148\u7ea7\u3002\u53e6\u4e00\u4e2a\u533a\u522b\u662f\u751f\u6210\u961f\u5217\u7684\u65b9\u5f0f\u3002\u5728\u4e0b\u9762\u7ed9\u51fa\u7684\u793a\u4f8b\u4e2d\uff0c\u5b83\u5c06\u4f7f\u7528\u4e24\u4e2a\u76f8\u540c\u7684\u6570\u636e\u96c6\u751f\u6210\u3002\n <\/div>\n

\u793a\u4f8b<\/h3>\n
\n \u4ee5\u4e0bPython\u7a0b\u5e8f\u53ef\u5e2e\u52a9\u5b9e\u73b0\u5177\u6709\u591a\u4e2a\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u961f\u5217-\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>threading
import <\/span>queue
import <\/span>random
import <\/span>time
def <\/span>myqueue(queue):
   while <\/span>not <\/span>queue.empty<\/span>():
      item = queue.get<\/span>()
      if <\/span>item is <\/span>None:
      break<\/span>
      print(\"{} removed {} from <\/span>the queue\"<\/span>.format<\/span>(threading.current_thread<\/span>(), item))
      queue.task_done<\/span>()
      time.sleep<\/span>(1)
q = queue.PriorityQueue<\/span>()
for <\/span><\/span>i in <\/span>range(5):
   q.put<\/span>(i,1)
for <\/span><\/span>i in <\/span>range(5):
   q.put<\/span>(i,1)
threads = []
for <\/span><\/span>i in <\/span>range(2):
   thread = threading.Thread<\/span>(target=myqueue, args=(q,))
   thread.start<\/span>()
   threads.append<\/span>(thread)
for <\/span><\/span>thread in <\/span>threads:
   thread.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u8f93\u51fa<\/h3>\n
\n
 <Thread(Thread-4939, started 2420)> removed 0 from <\/span>the queue
<Thread(Thread-4940, started 3284)> removed 0 from <\/span>the queue
<Thread(Thread-4939, started 2420)> removed 1 from <\/span>the queue
<Thread(Thread-4940, started 3284)> removed 1 from <\/span>the queue
<Thread(Thread-4939, started 2420)> removed 2 from <\/span>the queue
<Thread(Thread-4940, started 3284)> removed 2 from <\/span>the queue
<Thread(Thread-4939, started 2420)> removed 3 from <\/span>the queue
<Thread(Thread-4940, started 3284)> removed 3 from <\/span>the queue
<Thread(Thread-4939, started 2420)> removed 4 from <\/span>the queue
<Thread(Thread-4940, started 3284)> removed 4 from <\/span>the queue
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ebf\u7a0b\u901a\u4fe1zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[147],"tags":[],"class_list":["post-1579","post","type-post","status-publish","format-standard","hentry","category-python-dxc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1579"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1579"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1579\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}