or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

communication.mdcontext-config.mdindex.mdpools.mdprocess-management.mdshared-objects.mdsynchronization.md

process-management.mddocs/

0

# Process Management

1

2

Core functionality for creating, managing, and controlling processes in multiprocess. Provides a threading-like API for process lifecycle management with enhanced serialization capabilities.

3

4

## Capabilities

5

6

### Process Class

7

8

The main class for creating and managing child processes. Follows the same API as threading.Thread but uses separate processes instead of threads.

9

10

```python { .api }

11

class Process:

12

"""

13

A process object represents activity that is run in a separate process.

14

15

Args:

16

target: callable object to be invoked by the run() method

17

name: name of the process (defaults to Process-N)

18

args: argument tuple for the target invocation

19

kwargs: dictionary of keyword arguments for the target invocation

20

daemon: boolean flag indicating if process is daemonic

21

"""

22

def __init__(self, target=None, name=None, args=(), kwargs={}, daemon=None): ...

23

24

def start(self):

25

"""Start the process's activity."""

26

27

def run(self):

28

"""Method representing the process's activity."""

29

30

def terminate(self):

31

"""Terminate the process using SIGTERM."""

32

33

def kill(self):

34

"""Terminate the process using SIGKILL (Unix) or TerminateProcess (Windows)."""

35

36

def join(self, timeout=None):

37

"""

38

Wait until the process terminates.

39

40

Args:

41

timeout: optional timeout in seconds

42

"""

43

44

def is_alive(self):

45

"""

46

Return whether the process is alive.

47

48

Returns:

49

bool: True if process is running, False otherwise

50

"""

51

52

def close(self):

53

"""Close the Process object and release associated resources."""

54

55

# Properties

56

name: str # Process name

57

daemon: bool # Daemon flag

58

pid: int # Process ID (None if not started)

59

exitcode: int # Exit code (None if not terminated)

60

authkey: bytes # Authentication key

61

sentinel: object # Object that becomes ready when process terminates

62

```

63

64

### Process Inspection Functions

65

66

Functions for inspecting and managing process state at runtime.

67

68

```python { .api }

69

def current_process():

70

"""

71

Return the Process object corresponding to the current process.

72

73

Returns:

74

Process: The current process object

75

"""

76

77

def parent_process():

78

"""

79

Return the Process object corresponding to the parent process.

80

81

Returns:

82

Process: The parent process object, or None if no parent

83

"""

84

85

def active_children():

86

"""

87

Return a list of all live child processes.

88

89

Returns:

90

list[Process]: List of active child Process objects

91

"""

92

```

93

94

### System Information

95

96

Utility functions for system resource information.

97

98

```python { .api }

99

def cpu_count():

100

"""

101

Return the number of CPUs in the system.

102

103

Returns:

104

int: Number of CPUs available

105

106

Raises:

107

NotImplementedError: If CPU count cannot be determined

108

"""

109

```

110

111

### Platform Support

112

113

Platform-specific support functions for special deployment scenarios.

114

115

```python { .api }

116

def freeze_support():

117

"""

118

Add support for when a program which uses multiprocess has been

119

frozen to produce a Windows executable.

120

121

This should be called on the main thread at the start of the

122

main module for frozen Windows executables.

123

"""

124

```

125

126

## Usage Examples

127

128

### Basic Process Creation

129

130

```python

131

from multiprocess import Process

132

133

def worker_function(name, number):

134

print(f"Worker {name}: Processing {number}")

135

return number * 2

136

137

# Create and start a process

138

p = Process(target=worker_function, args=('Worker1', 42))

139

p.start()

140

p.join() # Wait for completion

141

print(f"Process completed with exit code: {p.exitcode}")

142

```

143

144

### Process with Custom Class

145

146

```python

147

from multiprocess import Process

148

149

class WorkerProcess(Process):

150

def __init__(self, data):

151

super().__init__()

152

self.data = data

153

154

def run(self):

155

print(f"Processing data: {self.data}")

156

# Do work here

157

print("Work completed")

158

159

# Create and start custom process

160

worker = WorkerProcess("important data")

161

worker.start()

162

worker.join()

163

```

164

165

### Managing Multiple Processes

166

167

```python

168

from multiprocess import Process, active_children

169

import time

170

171

def long_running_task(task_id):

172

print(f"Task {task_id} starting")

173

time.sleep(2)

174

print(f"Task {task_id} completed")

175

176

# Create multiple processes

177

processes = []

178

for i in range(3):

179

p = Process(target=long_running_task, args=(i,))

180

p.start()

181

processes.append(p)

182

183

# Monitor active processes

184

print(f"Active children: {len(active_children())}")

185

186

# Wait for all to complete

187

for p in processes:

188

p.join()

189

190

print("All processes completed")

191

```

192

193

### Process Properties and State

194

195

```python

196

from multiprocess import Process, current_process

197

import os

198

199

def show_process_info():

200

current = current_process()

201

print(f"Process name: {current.name}")

202

print(f"Process PID: {current.pid}")

203

print(f"OS PID: {os.getpid()}")

204

205

# Create process

206

p = Process(target=show_process_info, name='InfoWorker')

207

print(f"Before start - PID: {p.pid}, Alive: {p.is_alive()}")

208

209

p.start()

210

print(f"After start - PID: {p.pid}, Alive: {p.is_alive()}")

211

212

p.join()

213

print(f"After join - Exit code: {p.exitcode}, Alive: {p.is_alive()}")

214

```

215

216

### Daemon Processes

217

218

```python

219

from multiprocess import Process

220

import time

221

222

def daemon_worker():

223

while True:

224

print("Daemon working...")

225

time.sleep(1)

226

227

def regular_worker():

228

time.sleep(3)

229

print("Regular worker done")

230

231

# Create daemon process

232

daemon = Process(target=daemon_worker, daemon=True)

233

daemon.start()

234

235

# Create regular process

236

regular = Process(target=regular_worker)

237

regular.start()

238

regular.join()

239

240

print("Main process ending - daemon will be terminated")

241

# Daemon process terminates when main process exits

242

```