API Reference
process
Process
A class for spawning, managing, and interacting with a process synchronously.
This class provides a convenient interface for running a process, interacting with its standard input, output, and error streams, and managing its execution.
Source code in process/process.py
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
process
property
process: Popen
Return the underlying process instance.
Returns:
| Type | Description |
|---|---|
Popen
|
An instance of |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
running
property
running: bool
Check if the process is currently running.
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
stdin
property
stdin: StreamWriter
Return the standard input stream of the process if the process was created with stdin=PIPE.
Returns:
| Type | Description |
|---|---|
StreamWriter
|
The standard input stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
stdout
property
stdout: StreamReader
Return the standard output stream of the process if the process was created with stdout=PIPE.
Returns:
| Type | Description |
|---|---|
StreamReader
|
The standard output stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
stderr
property
stderr: StreamReader
Return the standard error stream of the process if the process was created with stderr=PIPE.
Returns:
| Type | Description |
|---|---|
StreamReader
|
The standard error stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
arguments
property
id
property
id: int
Return the process identifier.
Returns:
| Type | Description |
|---|---|
int
|
The process identifier. |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
exit_code
property
exit_code: int | None
Return the exit code of the process.
Returns:
| Type | Description |
|---|---|
int | None
|
The exit code of the process. If the process is still running, it returns |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
__init__
__init__(
arguments: StrOrPath | Sequence[StrOrPath],
stdin: bytes | File | None = None,
stdout: File | None = PIPE,
stderr: File | None = PIPE,
buffer_size: int | None = None,
) -> None
Initialize a new Process instance with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments |
StrOrPath | Sequence[StrOrPath]
|
The command and its arguments for the process. |
required |
stdin |
bytes | File | None
|
None
|
|
stdout |
File | None
|
PIPE
|
|
stderr |
File | None
|
PIPE
|
|
buffer_size |
int | None
|
The buffer size for the stream operations. If |
None
|
Notes
If arguments is an instance of str, it will be split into a list of arguments using shlex.split().
Warning
A file-like object without a valid file descriptor, such as BytesIO, cannot be used as stdin, stdout, or stderr.
Source code in process/process.py
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 | |
__del__
__del__() -> None
Clean up resources used by the process.
This method cleans up resources as follows:
- Close the standard input stream if the process was created with
stdin=PIPEand the stream is not closed. - If the process is running, terminate the process.
- Wait for the process to complete.
- Close the standard output stream if the process was created with
stdout=PIPEand the stream is not closed. - Close the standard error stream if the process was created with
stderr=PIPEand the stream is not closed.
Source code in process/process.py
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 | |
run
run() -> Self
Run the process.
Returns:
| Type | Description |
|---|---|
Self
|
The current |
Raises:
| Type | Description |
|---|---|
ProcessAlreadyRunError
|
If the process has already been run. |
ProcessError
|
If the process fails to run. |
Source code in process/process.py
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 | |
output
Return the output from the standard output stream of the process if the process was created with stdout=PIPE.
Warning
If the standard output stream of the process is modified by others, the result of output() will be affected by those changes.
with Process("echo something") as process:
print(process.stdout.read(4)) # b'some'
print(process.output()) # b'thing\n'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join |
bool
|
Whether to wait for the process to complete before returning the output. |
True
|
Returns:
| Type | Description |
|---|---|
bytes
|
The output from the standard output stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
Source code in process/process.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
error
Return the output from the standard error stream of the process if the process was created with stderr=PIPE.
Warning
If the standard error stream of the process is modified by others, the result of error() will be affected by those changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join |
bool
|
Whether to wait for the process to complete before returning the output. |
True
|
Returns:
| Type | Description |
|---|---|
bytes
|
The output from the standard error stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
Source code in process/process.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
join
join(timeout: float | None = None) -> None
Wait for the process to complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout |
float | None
|
Maximum time to wait for the process to complete, in seconds. If |
None
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessTimeoutError
|
If the process does not complete within the specified timeout. |
Source code in process/process.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
close
close() -> None
Close the process and release its resources.
This method cleans up the process execution as follows:
- Close the standard input stream if the process was created with
stdin=PIPEand the stream is not closed. - If the process is running, terminate it.
- Wait for the process to complete.
- Read the remaining output from the standard output stream and then close the stream if the process was created with
stdout=PIPEand the stream is not closed. - Read the remaining error from the standard error stream and then close the stream if the process was created with
stderr=PIPEand the stream is not closed.
Source code in process/process.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
__enter__
__enter__() -> Self
Enter the runtime context for this Process instance.
This method runs the process.
Returns:
| Type | Description |
|---|---|
Self
|
The current |
Source code in process/process.py
283 284 285 286 287 288 289 290 291 | |
__exit__
__exit__(
exception_type: type[BaseException] | None,
exception: BaseException | None,
traceback: TracebackType | None,
) -> None
Exit the runtime context for this Process instance.
This method cleans up the process execution as follows:
- Close the standard input stream if the process was created with
stdin=PIPEand the stream is not closed. - Wait for the process to complete.
- Read the remaining output from the standard output stream and then close the stream if the process was created with
stdout=PIPEand the stream is not closed. - Read the remaining error from the standard error stream and then close the stream if the process was created with
stderr=PIPEand the stream is not closed.
Source code in process/process.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
signal
signal(signal: int) -> None
Send a signal to the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal |
int
|
The signal number to send. |
required |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/process.py
194 195 196 197 198 199 200 201 202 | |
terminate
terminate() -> None
Gracefully terminate the process.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/process.py
205 206 207 208 209 210 211 | |
kill
kill() -> None
Forcefully kill the process.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/process.py
213 214 215 216 217 218 219 | |
__repr__
__repr__() -> str
Return a str representation of the current process instance.
Returns:
| Type | Description |
|---|---|
str
|
A |
Source code in process/process.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
types
process.asyncio
Process
A class for spawning, managing, and interacting with a process asynchronously.
This class provides a convenient interface for running a process, interacting with its standard input, output, and error streams, and managing its execution.
Source code in process/asyncio/process.py
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
process
property
process: Process
Return the underlying process object.
Returns:
| Type | Description |
|---|---|
Process
|
An instance of |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
running
property
running: bool
Check if the process is currently running.
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
stdin
property
stdin: StreamWriter
Return the standard input stream of the process if the process was created with stdin=PIPE.
Returns:
| Type | Description |
|---|---|
StreamWriter
|
The standard input stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
stdout
property
stdout: StreamReader
Return the standard output stream of the process if the process was created with stdout=PIPE.
Returns:
| Type | Description |
|---|---|
StreamReader
|
The standard output stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
stderr
property
stderr: StreamReader
Return the standard error stream of the process if the process was created with stderr=PIPE.
Returns:
| Type | Description |
|---|---|
StreamReader
|
The standard error stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
arguments
property
id
property
id: int
Return the process identifier.
Returns:
| Type | Description |
|---|---|
int
|
The process identifier. |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
exit_code
property
exit_code: int | None
Return the exit code of the process.
Returns:
| Type | Description |
|---|---|
int | None
|
The exit code of the process. If the process is still running, it returns |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
__init__
__init__(
arguments: StrOrPath | Sequence[StrOrPath],
stdin: bytes | File | None = None,
stdout: File | None = PIPE,
stderr: File | None = PIPE,
buffer_size: int | None = None,
) -> None
Initialize a new Process instance with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments |
StrOrPath | Sequence[StrOrPath]
|
The command and its arguments for the process. |
required |
stdin |
bytes | File | None
|
None
|
|
stdout |
File | None
|
PIPE
|
|
stderr |
File | None
|
PIPE
|
|
buffer_size |
int | None
|
The buffer size for the stream operations. If |
None
|
Notes
If arguments is an instance of str, it will be split into a list of arguments using shlex.split().
Warning
A file-like object without a valid file descriptor, such as BytesIO, cannot be used as stdin, stdout, or stderr.
Source code in process/asyncio/process.py
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 | |
__del__
__del__() -> None
Clean up resources used by the process.
This method cleans up resources as follows:
- Close the standard input stream if the process was created with
stdin=PIPEand the stream is not closed. - If the process is running, terminate the process.
Source code in process/asyncio/process.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
run
async
run() -> Self
Run the process.
Returns:
| Type | Description |
|---|---|
Self
|
The current |
Raises:
| Type | Description |
|---|---|
ProcessAlreadyRunError
|
If the process has already been run. |
ProcessError
|
If the process fails to run. |
Source code in process/asyncio/process.py
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 | |
output
async
Return the output from the standard output stream of the process if the process was created with stdout=PIPE.
Warning
If the standard output stream of the process is modified by others, the result of output() will be affected by those changes.
with Process("echo something") as process:
print(process.stdout.read(4)) # b'some'
print(process.output()) # b'thing\n'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join |
bool
|
Whether to wait for the process to complete before returning the output. |
True
|
Returns:
| Type | Description |
|---|---|
bytes
|
The output from the standard output stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
Source code in process/asyncio/process.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
error
async
Return the output from the standard error stream of the process if the process was created with stderr=PIPE.
Warning
If the standard error stream of the process is modified by others, the result of error() will be affected by those changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join |
bool
|
Whether to wait for the process to complete before returning the output. |
True
|
Returns:
| Type | Description |
|---|---|
bytes
|
The output from the standard error stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
Source code in process/asyncio/process.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
join
async
join(timeout: float | None = None) -> None
Wait for the process to complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout |
float | None
|
Maximum time to wait for the process to complete, in seconds. If |
None
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessTimeoutError
|
If the process does not complete within the specified timeout. |
Source code in process/asyncio/process.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
close
async
close() -> None
Close the process and release its resources.
This method cleans up the process execution as follows:
- Close the standard input stream if the process was created with
stdin=PIPEand the stream is not closed. - If the process is running, terminate it.
- Wait for the process to complete.
- Read the remaining output from the standard output stream if the process was created with
stdout=PIPE. - Read the remaining error from the standard error stream if the process was created with
stderr=PIPE.
Source code in process/asyncio/process.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
__aenter__
async
__aenter__() -> Self
Enter the runtime context for this Process instance.
This method runs the process.
Returns:
| Type | Description |
|---|---|
Self
|
The current |
Source code in process/asyncio/process.py
260 261 262 263 264 265 266 267 268 | |
__aexit__
async
__aexit__(
exception_type: type[BaseException] | None,
exception: BaseException | None,
traceback: TracebackType | None,
) -> None
Exit the runtime context for this Process instance.
This method cleans up the process execution as follows:
- Close the standard input stream if the process was created with
stdin=PIPEand the stream is not closed. - Wait for the process to complete.
- Read the remaining output from the standard output stream if the process was created with
stdout=PIPE. - Read the remaining error from the standard error stream if the process was created with
stderr=PIPE.
Source code in process/asyncio/process.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
signal
signal(signal: int) -> None
Send a signal to the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal |
int
|
The signal number to send. |
required |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/asyncio/process.py
194 195 196 197 198 199 200 201 202 203 | |
terminate
terminate() -> None
Gracefully terminate the process.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/asyncio/process.py
205 206 207 208 209 210 | |
kill
kill() -> None
Forcefully kill the process.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/asyncio/process.py
213 214 215 216 217 | |
__repr__
__repr__() -> str
Return a str representation of the current process instance.
Returns:
| Type | Description |
|---|---|
str
|
A |
Source code in process/asyncio/process.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
types
core
AbstractProcess
Abstract base class for managing processes.
Source code in process/core/process.py
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
arguments
property
process
abstractmethod
property
Return the underlying process object.
Returns:
| Type | Description |
|---|---|
Popen | Process
|
An instance of either |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
id
property
id: int
Return the process identifier.
Returns:
| Type | Description |
|---|---|
int
|
The process identifier. |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
running
abstractmethod
property
running: bool
Check if the process is currently running.
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
exit_code
property
exit_code: int | None
Return the exit code of the process.
Returns:
| Type | Description |
|---|---|
int | None
|
The exit code of the process. If the process is still running, it returns |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
stdin
abstractmethod
property
stdin: W
Return the standard input stream of the process if the process was created with stdin=PIPE.
Returns:
| Type | Description |
|---|---|
W
|
The standard input stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
stdout
abstractmethod
property
stdout: R
Return the standard output stream of the process if the process was created with stdout=PIPE.
Returns:
| Type | Description |
|---|---|
R
|
The standard output stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
stderr
abstractmethod
property
stderr: R
Return the standard error stream of the process if the process was created with stderr=PIPE.
Returns:
| Type | Description |
|---|---|
R
|
The standard error stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
__init__
__init__(
arguments: StrOrPath | Sequence[StrOrPath],
stdin: bytes | File | None = None,
stdout: File | None = PIPE,
stderr: File | None = PIPE,
buffer_size: int | None = None,
) -> None
Initialize a new Process instance with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments |
StrOrPath | Sequence[StrOrPath]
|
The command and its arguments for the process. |
required |
stdin |
bytes | File | None
|
None
|
|
stdout |
File | None
|
PIPE
|
|
stderr |
File | None
|
PIPE
|
|
buffer_size |
int | None
|
The buffer size for the stream operations. If |
None
|
Notes
If arguments is an instance of str, it will be split into a list of arguments using shlex.split().
Warning
A file-like object without a valid file descriptor, such as BytesIO, cannot be used as stdin, stdout, or stderr.
Source code in process/core/process.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 | |
__del__
abstractmethod
__del__() -> None
Clean up resources used by the process.
Source code in process/core/process.py
79 80 81 82 | |
run
abstractmethod
run() -> Returns[Self]
Run the process.
Returns:
| Type | Description |
|---|---|
Returns[Self]
|
The current |
Raises:
| Type | Description |
|---|---|
ProcessAlreadyRunError
|
If the process has already been run. |
ProcessError
|
If the process fails to run. |
Source code in process/core/process.py
159 160 161 162 163 164 165 166 167 168 169 170 | |
output
abstractmethod
Return the output from the standard output stream of the process if the process was created with stdout=PIPE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join |
bool
|
Whether to wait for the process to complete before returning the output. |
True
|
Returns:
| Type | Description |
|---|---|
Returns[bytes]
|
The output from the standard output stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
Source code in process/core/process.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
error
abstractmethod
Return the output from the standard error stream of the process if the process was created with stderr=PIPE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join |
bool
|
Whether to wait for the process to complete before returning the output. |
True
|
Returns:
| Type | Description |
|---|---|
Returns[bytes]
|
The output from the standard error stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
Source code in process/core/process.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
join
abstractmethod
join(timeout: float | None = None) -> Returns[None]
Wait for the process to complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout |
float | None
|
Maximum time to wait for the process to complete, in seconds. If |
None
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessTimeoutError
|
If the process does not complete within the specified timeout. |
Source code in process/core/process.py
204 205 206 207 208 209 210 211 212 213 214 215 | |
signal
signal(signal: int) -> None
Send a signal to the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal |
int
|
The signal number to send. |
required |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/core/process.py
217 218 219 220 221 222 223 224 225 226 | |
terminate
terminate() -> None
Gracefully terminate the process.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/core/process.py
228 229 230 231 232 233 234 | |
kill
kill() -> None
Forcefully kill the process.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/core/process.py
236 237 238 239 240 241 242 | |
close
abstractmethod
close() -> Returns[None]
Close the process and release its resources.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/core/process.py
244 245 246 247 248 249 250 | |
__repr__
__repr__() -> str
Return a str representation of the current process instance.
Returns:
| Type | Description |
|---|---|
str
|
A |
Source code in process/core/process.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
ProcessProtocol
A protocol that defines the interface for spawning and managing a process.
Source code in process/core/protocol.py
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
arguments
property
process
property
Return the underlying process instance.
Returns:
| Type | Description |
|---|---|
Popen | Process
|
An instance of either |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
id
property
id: int
Return the process identifier.
Returns:
| Type | Description |
|---|---|
int
|
The process identifier. |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
running
property
running: bool
Check if the process is currently running.
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
exit_code
property
exit_code: int | None
Return the exit code of the process.
Returns:
| Type | Description |
|---|---|
int | None
|
The exit code of the process. If the process is still running, it returns |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
stdin
property
stdin: W_co
Return the standard input stream of the process.
Returns:
| Type | Description |
|---|---|
W_co
|
The standard input stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
stdout
property
stdout: R_co
Return the standard output stream of the process.
Returns:
| Type | Description |
|---|---|
R_co
|
The standard output stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
stderr
property
stderr: R_co
Return the standard error stream of the process.
Returns:
| Type | Description |
|---|---|
R_co
|
The standard error stream of the process if the process was created with |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
__init__
__init__(
arguments: StrOrPath | Sequence[StrOrPath],
stdin: bytes | File | None = None,
stdout: File | None = PIPE,
stderr: File | None = PIPE,
buffer_size: int | None = None,
) -> None
Initialize a new Process instance with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arguments |
StrOrPath | Sequence[StrOrPath]
|
The command and its arguments for the process. |
required |
stdin |
bytes | File | None
|
None
|
|
stdout |
File | None
|
PIPE
|
|
stderr |
File | None
|
PIPE
|
|
buffer_size |
int | None
|
The buffer size for the stream operations. If |
None
|
Notes
If arguments is an instance of str, it will be split into a list of arguments using shlex.split().
Warning
A file-like object without a valid file descriptor, such as BytesIO, cannot be used as stdin, stdout, or stderr.
Source code in process/core/protocol.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
__del__
__del__() -> None
Clean up resources used by the process.
Source code in process/core/protocol.py
70 71 72 | |
run
run() -> Returns[Self]
Run the process.
Returns:
| Type | Description |
|---|---|
Returns[Self]
|
The current |
Raises:
| Type | Description |
|---|---|
ProcessAlreadyRunError
|
If the process has already been run. |
ProcessError
|
If the process fails to run. |
Source code in process/core/protocol.py
146 147 148 149 150 151 152 153 154 155 156 | |
output
Return the output from the standard output stream of the process if the process was created with stdout=PIPE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join |
bool
|
Whether to wait for the process to complete before returning the output. |
True
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
Source code in process/core/protocol.py
158 159 160 161 162 163 164 165 166 167 168 | |
error
Return the output from the standard error stream of the process if the process was created with stderr=PIPE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
join |
bool
|
Whether to wait for the process to complete before returning the output. |
True
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessInvalidStreamError
|
If the process was not created with |
Source code in process/core/protocol.py
170 171 172 173 174 175 176 177 178 179 180 | |
join
join(timeout: float | None = None) -> Returns[None]
Wait for the process to complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout |
float | None
|
Maximum time to wait for the process to complete, in seconds. If |
None
|
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
ProcessTimeoutError
|
If the process does not complete within the specified timeout. |
Source code in process/core/protocol.py
182 183 184 185 186 187 188 189 190 191 192 | |
signal
signal(signal: int) -> None
Send a signal to the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal |
int
|
The signal number to send. |
required |
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/core/protocol.py
194 195 196 197 198 199 200 201 202 203 | |
terminate
terminate() -> None
Gracefully terminate the process.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/core/protocol.py
205 206 207 208 209 210 211 | |
kill
kill() -> None
Forcefully kill the process.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/core/protocol.py
213 214 215 216 217 218 219 | |
close
close() -> Returns[None]
Close the process and release its resources.
Raises:
| Type | Description |
|---|---|
ProcessNotRunError
|
If the process has not been run. |
Source code in process/core/protocol.py
221 222 223 224 225 226 | |
types
⚠️ Errors
ProcessError
Base class for exceptions related to process errors.
Source code in process/core/errors.py
1 2 | |
ProcessNotRunError
Exception raised when a process has not been run.
Source code in process/core/errors.py
5 6 | |
ProcessAlreadyRunError
Exception raised when the process has already been run.
Source code in process/core/errors.py
9 10 | |
ProcessInvalidStreamError
Exception raised when an invalid stream is encountered in the process.
Source code in process/core/errors.py
13 14 | |
ProcessTimeoutError
Exception raised when the process times out.
Source code in process/core/errors.py
17 18 | |
🔢 Constants
PIPE
module-attribute
PIPE = PIPE
Special value that can be used as the stdin, stdout, or stderr argument to indicate that a pipe to the standard stream should be opened.
STDOUT
module-attribute
STDOUT = STDOUT
Special value that can be used as the stderr argument to indicate that standard error should be redirected to standard output.
DEVNULL
module-attribute
DEVNULL = DEVNULL
Special value that can be used as the stdin, stdout, or stderr argument to indicate that the special file os.devnull should be used.
DEFAULT_BUFFER_SIZE
module-attribute
DEFAULT_BUFFER_SIZE = DEFAULT_BUFFER_SIZE
The default buffer size used by the io streams.
asyncio
DEFAULT_BUFFER_SIZE
module-attribute
DEFAULT_BUFFER_SIZE = 2 ** 16
The default buffer size used by the asyncio streams.