{"id":1587,"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\u4e8b\u4ef6\u9a71\u52a8","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1587.html","title":{"rendered":"Python\u7ebf\u7a0b\u4e8b\u4ef6\u9a71\u52a8"},"content":{"rendered":"
\n
Python\u7ebf\u7a0b\u4e8b\u4ef6\u9a71\u52a8\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n loop = get_event_loop()<\/b>-\u6b64\u65b9\u6cd5\u5c06\u4e3a\u5f53\u524d\u4e0a\u4e0b\u6587\u63d0\u4f9b\u4e8b\u4ef6\u5faa\u73af\u3002<\/span> result()\u548cexception()\u65b9\u6cd5\u4e0d\u4f1a\u4f7f\u7528\u8d85\u65f6\u53c2\u6570\uff0c\u5e76\u4e14\u5728\u672a\u6765\u8fd8\u6ca1\u6709\u5b8c\u6210\u65f6\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/span> ReadTransport <\/b>-\u8fd9\u662f\u53ea\u8bfb\u4f20\u8f93\u7684\u63a5\u53e3\u3002<\/span> close()<\/b>-\u5173\u95ed\u4f20\u8f93\u3002<\/span> \u534f\u8bae<\/b>-\u8fd9\u662f\u5b9e\u73b0\u7528\u4e8eTCP\u548cSSL\u4f20\u8f93\u7684\u6d41\u534f\u8bae\u7684\u57fa\u7c7b\u3002<\/span> DatagramProtocol <\/b>-\u8fd9\u662f\u7528\u4e8e\u5b9e\u73b0\u4e0eUDP\u4f20\u8f93\u4e00\u8d77\u4f7f\u7528\u7684\u6570\u636e\u62a5\u534f\u8bae\u7684\u57fa\u7c7b\u3002<\/span> SubprocessProtocol <\/b>-\u8fd9\u662f\u7528\u4e8e\u5b9e\u73b0\u901a\u8fc7\u4e00\u7ec4\u5355\u5411\u7ba1\u9053\u4e0e\u5b50\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1\u7684\u534f\u8bae\u7684\u57fa\u7c7b\u3002<\/span> <\/span> <\/p>\n
Python\u6a21\u5757\u2013 Asyncio <\/h2>\n
\u4e8b\u4ef6\u5faa\u73af<\/h3>\n
\n loop.call_later\uff08time_delay\uff0ccallback\uff0cargument\uff09<\/b>-\u6b64\u65b9\u6cd5\u5b89\u6392\u5728\u7ed9\u5b9atime_delay\u79d2\u540e\u8c03\u7528\u7684\u56de\u8c03\u3002<\/span>
\n loop.call_soon\uff08callback\uff0cargument\uff09<\/b>-\u6b64\u65b9\u6cd5\u5b89\u6392\u4e86\u4e00\u4e2a\u5c3d\u5feb\u88ab\u8c03\u7528\u7684\u56de\u8c03\u3002\u5728call_soon()\u8fd4\u56de\u4e4b\u540e\u4ee5\u53ca\u63a7\u4ef6\u8fd4\u56de\u4e8b\u4ef6\u5faa\u73af\u65f6\u8c03\u7528\u8be5\u56de\u8c03\u3002<\/span>
\n loop.time()<\/b>-\u6b64\u65b9\u6cd5\u7528\u4e8e\u6839\u636e\u4e8b\u4ef6\u5faa\u73af\u7684\u5185\u90e8\u65f6\u949f\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u3002<\/span>
\n asyncio.set_event_loop()<\/b>-\u6b64\u65b9\u6cd5\u4f1a\u5c06\u5f53\u524d\u4e0a\u4e0b\u6587\u7684\u4e8b\u4ef6\u5faa\u73af\u8bbe\u7f6e\u4e3a\u5faa\u73af\u3002<\/span>
\n asyncio.new_event_loop()<\/b>-\u6b64\u65b9\u6cd5\u5c06\u521b\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u4e8b\u4ef6\u5faa\u73af\u5bf9\u8c61\u3002<\/span>
\n loop.run_forever()<\/b>-\u6b64\u65b9\u6cd5\u5c06\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u8c03\u7528stop()\u65b9\u6cd5\u4e3a\u6b62\u3002<\/span> <\/p>\n\u793a\u4f8b<\/h3>\n
\n hello world <\/b>\u3002\u6b64\u793a\u4f8b\u6458\u81eaPython\u5b98\u65b9\u6587\u6863\u3002\n <\/div>\n # Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>asyncio
def <\/span>hello_world(loop):
print('Hello World'<\/span>)
loop.stop<\/span>()
loop = asyncio.get_event_loop<\/span>()
loop.call_soon<\/span>(hello_world, loop)
loop.run_forever<\/span>()
loop.close<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
Hello World
<\/span><\/code><\/pre>\n<\/p><\/div>\nFuture<\/h3>\n
\n \u5728add_done_callback()\u4e2d\u6ce8\u518c\u7684\u56de\u8c03\u603b\u662f\u901a\u8fc7\u4e8b\u4ef6\u5faa\u73af\u7684call_soon()\u8fdb\u884c\u8c03\u7528\u3002<\/span>
\n asyncio.futures.Future\u7c7b\u4e0e\u5e76\u53d1.futures\u5305\u4e2d\u7684wait()\u548cas_completed()\u51fd\u6570\u4e0d\u517c\u5bb9\u3002<\/span> <\/p>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>asyncio
async def <\/span>Myoperation(future):
await asyncio.sleep<\/span>(2)
future.set_result<\/span>('Future Completed'<\/span>)
loop = asyncio.get_event_loop<\/span>()
future = asyncio.Future<\/span>()
asyncio.ensure_future<\/span>(Myoperation(future))
try:
loop.run_until_complete<\/span>(future)
print(future.result<\/span>())
finally:
loop.close<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
Future Completed
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u534f\u7a0b<\/h3>\n
\u5f02\u6b65def\u51fd\u6570()<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>asyncio
async def <\/span>Myoperation():
print(\"First Coroutine\"<\/span>)
loop = asyncio.get_event_loop<\/span>()
try:
loop.run_until_complete<\/span>(Myoperation())
finally:
loop.close<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
First Coroutine
<\/span><\/code><\/pre>\n<\/p><\/div>\n @asyncio.coroutine\u88c5\u9970\u5668<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>asyncio
@asyncio.coroutine
def <\/span>Myoperation():
print(\"First Coroutine\"<\/span>)
loop = asyncio.get_event_loop<\/span>()
try:
loop.run_until_complete<\/span>(Myoperation())
finally:
loop.close<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
First Coroutine
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u4efb\u52a1<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>asyncio
import <\/span>time
async def <\/span>Task_ex(n):
time.sleep<\/span>(1)
print(\"Processing {}\"<\/span>.format<\/span>(n))
async def <\/span>Generator_task():
for <\/span><\/span>i in <\/span>range(10):
asyncio.ensure_future<\/span>(Task_ex(i))
int(\"Tasks Completed\"<\/span>)
asyncio.sleep<\/span>(2)
loop = asyncio.get_event_loop<\/span>()
loop.run_until_complete<\/span>(Generator_task())
loop.close<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
Tasks Completed
Processing 0
Processing 1
Processing 2
Processing 3
Processing 4
Processing 5
Processing 6
Processing 7
Processing 8
Processing 9
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8fd0\u8f93<\/h3>\n
\n WriteTransport <\/b>-\u8fd9\u662f\u4ec5\u5199\u4f20\u8f93\u7684\u63a5\u53e3\u3002<\/span>
\n DatagramTransport <\/b>-\u8fd9\u662f\u7528\u4e8e\u53d1\u9001\u6570\u636e\u7684\u63a5\u53e3\u3002<\/span>
\n BaseSubprocessTransport <\/b>-\u7c7b\u4f3c\u4e8eBaseTransport\u7c7b\u3002<\/span> <\/p>\n
\n is_closing()<\/b>-\u5982\u679c\u4f20\u8f93\u6b63\u5728\u5173\u95ed\u6216\u5df2\u7ecf\u5173\u95ed\uff0c\u5219\u6b64\u65b9\u6cd5\u5c06\u8fd4\u56detrue\u3002 get_extra_info\uff08name\uff0cdefault = none\uff09<\/b>-\u8fd9\u5c06\u4e3a\u6211\u4eec\u63d0\u4f9b\u6709\u5173\u8fd0\u8f93\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/span> get_protocol()<\/b>-\u6b64\u65b9\u6cd5\u5c06\u8fd4\u56de\u5f53\u524d\u534f\u8bae\u3002<\/span> <\/p>\n\u534f\u8bae<\/h3>\n
\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ebf\u7a0b\u4e8b\u4ef6\u9a71\u52a8zh-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-1587","post","type-post","status-publish","format-standard","hentry","category-python-dxc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1587"}],"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=1587"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1587\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}