cillow.Server
Cillow server component.
This class is responsible for managing request workers and client manager.
max_interpreters
limits the total number of processes that can be created.interpreters_per_client
limits the number of processes that can be created per client.num_worker_threads
limits the number of request worker threads.max_queue_size
limits the maximum size of the request queue.
Examples:
>>> import cillow
>>>
>>> if __name__ == "__main__":
... server = cillow.Server(port=5556, max_interpreters=2, interpreters_per_client=1)
... server.run()
Don't trust LLMS? Concerned about arbitrary code execution? Take full control by limiting functionalities using patches.
To add patches, use the add_patches()
function. To clear patches, use clear_patches()
.
Examples:
>>> import cillow
>>>
>>> import os
>>> from contextlib import contextmanager
>>>
>>> os_system_switchable = cillow.Switchable(os.system)
>>>
>>> @contextmanager
... def patch_os_system():
... def disabled_os_system(command: str):
... return "os.system has been disabled."
...
... with os_system_switchable.switch_to(disabled_os_system):
... yield
...
>>> cillow.add_patches(
... patch_os_system, # Disable os.system
... cillow.prebuilt_patches.patch_stdout_stderr_write, # To capture stdout and stderr
... cillow.prebuilt_patches.patch_matplotlib_pyplot_show, # To capture matplotlib figures
... cillow.prebuilt_patches.patch_pillow_show, # To capture PIL images
... )
>>>
>>> if __name__ == "__main__":
... server = cillow.Server(port=5556, max_interpreters=2, interpreters_per_client=1)
... server.run()
Source code in cillow/server/__init__.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
run
¶
Run the server and block until interrupted.
Source code in cillow/server/__init__.py
Client Manager¶
Manages clients and their interpreter processes.
This class is utilized by the server component to share the instance with all the request worker threads.
Source code in cillow/server/client_manager.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
optimal_max_queue_size
property
¶
Get optimal maximum queue size based on current limits.
optimal_number_of_request_workers
property
¶
Optimal number of request worker threads based on current limits.
total_active_processes
property
¶
Get total number of active interpreter processes.
cleanup
¶
delete_interpreter
¶
Delete client's interpreter processes at the given environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
The client identifier |
required |
environment
|
PythonEnvironment | str
|
The environment associated with the interpreter |
required |
Source code in cillow/server/client_manager.py
get_info
¶
register
¶
Register a client if possible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
The client identifier |
required |
environment
|
PythonEnvironment | str
|
The environment to use. This environment will be used as default when an interpreter process is deleted. |
'$system'
|
Raises:
Type | Description |
---|---|
Exception
|
If the client limit is exceeded. |
LookupError
|
If the given environment is invalid or not found. |
Source code in cillow/server/client_manager.py
remove
¶
Remove a client and stop all its interpreter processes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
The client identifier |
required |
Source code in cillow/server/client_manager.py
switch_interpreter
¶
Switch client to interpreter process based on the given environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
The client identifier |
required |
environment
|
PythonEnvironment | str
|
The environment to switch to |
required |
Raises:
Type | Description |
---|---|
Exception
|
If unable to create new interpreter due to process limit |
LookupError
|
If the given environment is invalid or not found |
ValueError
|
If client is not found |
Returns:
Type | Description |
---|---|
PythonEnvironment
|
The valid Python environment value |
Source code in cillow/server/client_manager.py
Request Worker¶
Request worker thread to handle incoming requests from clients.
Source code in cillow/server/request_worker.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
run
¶
Run the worker thread.