Web to py enterprise web framework - p 15

EXECUTION ENVIRONMENT 125 models and controllers are designed to be executed in a prepared environment that has been prepopulated with web2py global objects (request, response, session, cache and T) and helper functions. This is necessary because Python is a statically (lexically) scoped language, whereas the web2py environment is created dynamically. web2py provides the exec environment function to allow you to access models and controllers directly. exec environment creates a web2py execution environment, loads the file into it and then returns a Storage object containing the environment. The Storage object also serves as a namespacing mechanism. Any Python file designed to be executed. | EXECUTION ENVIRONMENT 125 models and controllers are designed to be executed in a prepared environment that has been prepopulated with WEB2py global objects request response session cache and T and helper functions. This is necessary because Python is a statically lexically scoped language whereas the WEB2py environment is created dynamically. WEB2py provides the exec_environment function to allow you to access models and controllers directly. exec_environment creates a WEB2py execution environment loads the file into it and then returns a Storage object containing the environment. The Storage object also serves as a namespacing mechanism. Any Python file designed to be executed in the execution environment can be loaded using exe environment. Uses for exe environment include Accessing data models from other applications. Accessing global objects from other models or controllers. Executing controller functions from other controllers. Loading site-wide helper libraries. This example reads rows from the user table in the cas application 1 from import exec_environment 2 cas exec_environment applications cas models 3 rows cas .db .select Another example suppose you have a controller that contains 1 def some_action 2 return dict remote_addr Here is how you can call this action from another controller or from the WEB2py shell 1 from import exec_environment 2 other exec_environment applications app controllers request request 3 result In line 2 request request is optional. It has the effect of passing the current request to the environment of other . Without this argument the environment would contain a new and empty apart from request object. It is also possible to pass a response and a session object to exe environment. Be careful when passing request response and session objects modification by the called action or coding dependencies in the called .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.