5 cấp độ sử dụng trình quản lý ngữ cảnh trong Python

Một trong những tính năng Python khó hiểu nhất đối với người mới bắt đầu là câu lệnh "with", liên quan đến trình quản lý ngữ cảnh vì không phải ngôn ngữ lập trình nào cũng có cú pháp này

Quản lý tài nguyên. Trong bất kỳ ngôn ngữ lập trình nào, việc sử dụng các tài nguyên như thao tác tệp hoặc kết nối cơ sở dữ liệu là rất phổ biến. Nhưng những tài nguyên này bị hạn chế về nguồn cung. Do đó, vấn đề chính nằm ở việc đảm bảo giải phóng các tài nguyên này sau khi sử dụng. Nếu chúng không được giải phóng thì sẽ dẫn đến rò rỉ tài nguyên và có thể khiến hệ thống chạy chậm lại hoặc gặp sự cố. Sẽ rất hữu ích nếu người dùng có cơ chế tự động thiết lập và chia nhỏ tài nguyên. Trong Python, điều này có thể đạt được bằng cách sử dụng các trình quản lý bối cảnh tạo điều kiện thuận lợi cho việc xử lý tài nguyên hợp lý. Cách phổ biến nhất để thực hiện các thao tác với tệp là sử dụng từ khóa như hình bên dưới.  

Python3




init method called
enter method called
with statement block
exit method called
6

init method called
enter method called
with statement block
exit method called
7

 

_______48____49____50

init method called
enter method called
with statement block
exit method called
1____52
init method called
enter method called
with statement block
exit method called
0
init method called
enter method called
with statement block
exit method called
1

Hãy lấy ví dụ về quản lý tệp. Khi một tệp được mở, một bộ mô tả tệp được sử dụng, đây là một tài nguyên hạn chế. Tại một thời điểm, chỉ một số tệp nhất định có thể được mở bởi một quy trình. Chương trình sau đây chứng minh điều đó.  

Python3




init method called
enter method called
with statement block
exit method called
2
init method called
enter method called
with statement block
exit method called
0
init method called
enter method called
with statement block
exit method called
4

init method called
enter method called
with statement block
exit method called
5
init method called
enter method called
with statement block
exit method called
6
init method called
enter method called
with statement block
exit method called
7
init method called
enter method called
with statement block
exit method called
8
init method called
enter method called
with statement block
exit method called
9
init method called
enter method called
with statement block
exit method called
00
init method called
enter method called
with statement block
exit method called
01

init method called
enter method called
with statement block
exit method called
1
init method called
enter method called
with statement block
exit method called
03____49
init method called
enter method called
with statement block
exit method called
9
init method called
enter method called
with statement block
exit method called
06
init method called
enter method called
with statement block
exit method called
07
init method called
enter method called
with statement block
exit method called
08
init method called
enter method called
with statement block
exit method called
09

đầu ra

init method called
enter method called
with statement block
exit method called
8

Thông báo lỗi nói rằng có quá nhiều tệp đang mở. Ví dụ trên là trường hợp rò rỉ bộ mô tả tệp. Nó xảy ra vì có quá nhiều tệp đang mở và chúng không được đóng. Có thể có khả năng một lập trình viên có thể quên đóng một tệp đã mở.  

Quản lý tài nguyên bằng trình quản lý ngữ cảnh. Giả sử một khối mã phát sinh một ngoại lệ hoặc nếu nó có một thuật toán phức tạp với nhiều đường dẫn trả về, thì việc đóng một tệp ở tất cả các vị trí sẽ trở nên khó khăn. Nói chung, trong các ngôn ngữ khác khi làm việc với tệp, try-ngoại trừ cuối cùng được sử dụng để đảm bảo rằng tài nguyên tệp được đóng sau khi sử dụng ngay cả khi có ngoại lệ. Python cung cấp một cách dễ dàng để quản lý tài nguyên. Trình quản lý bối cảnh. Từ khóa with được sử dụng. Khi nó được đánh giá, nó sẽ dẫn đến một đối tượng thực hiện quản lý bối cảnh. Trình quản lý bối cảnh có thể được viết bằng các lớp hoặc hàm [có trang trí]

Tạo Trình quản lý ngữ cảnh. Khi tạo trình quản lý ngữ cảnh bằng các lớp, người dùng cần đảm bảo rằng lớp đó có các phương thức. __enter__[] và __exit__[]. __enter__[] trả về tài nguyên cần được quản lý và __exit__[] không trả về gì ngoài thực hiện các hoạt động dọn dẹp. Đầu tiên, chúng ta hãy tạo một lớp đơn giản gọi là ContextManager để hiểu cấu trúc cơ bản của việc tạo trình quản lý ngữ cảnh bằng cách sử dụng các lớp, như minh họa bên dưới.  

Python3




init method called
enter method called
with statement block
exit method called
60

init method called
enter method called
with statement block
exit method called
61

 

init method called
enter method called
with statement block
exit method called
62
init method called
enter method called
with statement block
exit method called
63

init method called
enter method called
with statement block
exit method called
1
init method called
enter method called
with statement block
exit method called
65
init method called
enter method called
with statement block
exit method called
66
init method called
enter method called
with statement block
exit method called
67
init method called
enter method called
with statement block
exit method called
01

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
70____09
init method called
enter method called
with statement block
exit method called
72
init method called
enter method called
with statement block
exit method called
73

init method called
enter method called
with statement block
exit method called
69

init method called
enter method called
with statement block
exit method called
1
init method called
enter method called
with statement block
exit method called
65
init method called
enter method called
with statement block
exit method called
77
init method called
enter method called
with statement block
exit method called
67
init method called
enter method called
with statement block
exit method called
01

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
70____09
init method called
enter method called
with statement block
exit method called
83
init method called
enter method called
with statement block
exit method called
73

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
86
init method called
enter method called
with statement block
exit method called
67

init method called
enter method called
with statement block
exit method called
1

init method called
enter method called
with statement block
exit method called
1____465
init method called
enter method called
with statement block
exit method called
91____467
init method called
enter method called
with statement block
exit method called
93

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
70____09
init method called
enter method called
with statement block
exit method called
97
init method called
enter method called
with statement block
exit method called
73

 

init method called
enter method called
with statement block
exit method called
99

init method called
enter method called
with statement block
exit method called
1____470____09
init method called
enter method called
with statement block
exit method called
03
init method called
enter method called
with statement block
exit method called
73

đầu ra

init method called
enter method called
with statement block
exit method called

Trong trường hợp này, một đối tượng ContextManager được tạo. Điều này được gán cho biến sau từ khóa i. quản lý điện tử. Khi chạy chương trình trên, phần sau được thực hiện theo trình tự

  • __trong đó__[]
  • __đi vào__[]
  • thân câu lệnh [mã bên trong khối with]
  • __exit__[][các tham số trong phương thức này được sử dụng để quản lý các ngoại lệ]

Quản lý tệp bằng trình quản lý bối cảnh. Hãy áp dụng khái niệm trên để tạo một lớp giúp quản lý tài nguyên tệp. Lớp Trình quản lý tệp giúp mở tệp, ghi/đọc nội dung và sau đó đóng tệp.  

Python3




init method called
enter method called
with statement block
exit method called
6

init method called
enter method called
with statement block
exit method called
06

init method called
enter method called
with statement block
exit method called
61

 

init method called
enter method called
with statement block
exit method called
62
init method called
enter method called
with statement block
exit method called
09

init method called
enter method called
with statement block
exit method called
1
init method called
enter method called
with statement block
exit method called
65
init method called
enter method called
with statement block
exit method called
66
init method called
enter method called
with statement block
exit method called
67
init method called
enter method called
with statement block
exit method called
14

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
67____517
init method called
enter method called
with statement block
exit method called
0
init method called
enter method called
with statement block
exit method called
19

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
67____522
init method called
enter method called
with statement block
exit method called
0
init method called
enter method called
with statement block
exit method called
24

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
67____527
init method called
enter method called
with statement block
exit method called
28
init method called
enter method called
with statement block
exit method called
0
init method called
enter method called
with statement block
exit method called
00

init method called
enter method called
with statement block
exit method called
69

init method called
enter method called
with statement block
exit method called
1
init method called
enter method called
with statement block
exit method called
65
init method called
enter method called
with statement block
exit method called
77
init method called
enter method called
with statement block
exit method called
67
init method called
enter method called
with statement block
exit method called
01

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
67____527
init method called
enter method called
with statement block
exit method called
28
init method called
enter method called
with statement block
exit method called
0
init method called
enter method called
with statement block
exit method called
9
init method called
enter method called
with statement block
exit method called
9
init method called
enter method called
with statement block
exit method called
67
init method called
enter method called
with statement block
exit method called
15
init method called
enter method called
with statement block
exit method called
67
init method called
enter method called
with statement block
exit method called
17

init method called
enter method called
with statement block
exit method called
69
init method called
enter method called
with statement block
exit method called
86
init method called
enter method called
with statement block
exit method called
67
init method called
enter method called
with statement block
exit method called
27
init method called
enter method called
with statement block
exit method called
28

init method called
enter method called
with statement block
exit method called
1

init method called
enter method called
with statement block
exit method called
1____465
init method called
enter method called
with statement block
exit method called
91____467
init method called
enter method called
with statement block
exit method called
93

init method called
enter method called
with statement block
exit method called
69____467____527
init method called
enter method called
with statement block
exit method called
28
init method called
enter method called
with statement block
exit method called
33

 

init method called
enter method called
with statement block
exit method called
34

init method called
enter method called
with statement block
exit method called
35
init method called
enter method called
with statement block
exit method called
06____107
init method called
enter method called
with statement block
exit method called
08
init method called
enter method called
with statement block
exit method called
39

_______51____041____042

init method called
enter method called
with statement block
exit method called
73

 

init method called
enter method called
with statement block
exit method called
70
init method called
enter method called
with statement block
exit method called
45

đầu ra

init method called
enter method called
with statement block
exit method called
0

Quản lý tệp bằng trình quản lý ngữ cảnh và có câu lệnh. Khi thực hiện khối with, các hoạt động sau đây sẽ diễn ra theo trình tự

  • Một đối tượng FileManager được tạo bằng test. txt làm tên tệp và w[write] làm chế độ khi phương thức __init__ được thực thi
  • Phương thức __enter__ mở bài kiểm tra. txt ở chế độ ghi [thao tác thiết lập] và trả về một đối tượng tệp cho biến f
  • Văn bản 'Kiểm tra' được ghi vào tệp
  • Phương thức __exit__ đảm nhiệm việc đóng tệp khi thoát khỏi khối with [thao tác xé nhỏ]. Khi in[f. đã đóng] được chạy, đầu ra là True vì Trình quản lý tệp đã đảm nhiệm việc đóng tệp mà nếu không thì cần phải thực hiện rõ ràng

Quản lý kết nối cơ sở dữ liệu bằng trình quản lý bối cảnh và với câu lệnh. Khi thực hiện khối with, các hoạt động sau đây sẽ diễn ra theo trình tự

Trình quản lý ngữ cảnh trong Python là gì?

Trình quản lý bối cảnh thường đảm nhận việc thiết lập một số tài nguyên, chẳng hạn như. g. mở một kết nối và tự động xử lý việc dọn dẹp khi chúng tôi hoàn tất . Có lẽ, trường hợp sử dụng phổ biến nhất là mở tệp. với open['/path/to/file. txt', 'r'] dưới dạng f. cho dòng trong f. in[dòng]

Những từ khóa nào được sử dụng để kích hoạt trình quản lý ngữ cảnh trong Python?

Python cung cấp một cách dễ dàng để quản lý tài nguyên. Trình quản lý bối cảnh. Từ khóa with được sử dụng. Khi nó được đánh giá, nó sẽ dẫn đến một đối tượng thực hiện quản lý bối cảnh.

Làm cách nào để bạn tạo một trình quản lý bối cảnh lớp A trong Python?

Trong phương pháp này, có 5 bước để triển khai trình quản lý ngữ cảnh của riêng bạn. .
Xác định một chức năng
[tùy chọn] Viết bất kỳ mã thiết lập nào mà ngữ cảnh của bạn cần
Sử dụng từ khóa năng suất
[tùy chọn] Viết bất kỳ mã phân tích nào mà ngữ cảnh của bạn cần
Thêm @contextlib. trình trang trí contextmanager

Những phương pháp nào nên được xác định ít nhất để xác định trình quản lý ngữ cảnh?

Triển khai Trình quản lý bối cảnh dưới dạng Lớp. Ít nhất thì trình quản lý ngữ cảnh có phương thức __enter__ và __exit__ được xác định. Hãy tạo Trình quản lý bối cảnh mở tệp của riêng chúng tôi và tìm hiểu những điều cơ bản. Chỉ bằng cách định nghĩa các phương thức __enter__ và __exit__, chúng ta có thể sử dụng lớp mới của mình trong câu lệnh with

Chủ Đề