or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

communication-framework.mdconnection-management.mdcore-kernel.mddata-utilities.mdgui-integration.mdin-process-kernels.mdindex.mdio-streaming.mdkernel-application.mdkernel-embedding.mdmatplotlib-integration.md

in-process-kernels.mddocs/

0

# In-Process Kernels

1

2

Complete in-process kernel implementation for scenarios requiring tight integration without separate processes. Provides kernel functionality within the same Python process as the calling code, enabling synchronous interaction and shared memory access.

3

4

## Capabilities

5

6

### In-Process Kernel Manager

7

8

Manager for in-process kernel lifecycle and configuration.

9

10

```python { .api }

11

class InProcessKernelManager:

12

"""

13

Manager for in-process kernels.

14

15

Provides kernel lifecycle management within the same Python process

16

as the client, enabling direct access and synchronous operations.

17

"""

18

19

def start_kernel(self, **kwargs):

20

"""

21

Start the in-process kernel.

22

23

Parameters:

24

- **kwargs: Kernel configuration options

25

"""

26

27

def restart_kernel(self, now=False, newports=False, **kwargs):

28

"""

29

Restart the in-process kernel.

30

31

Parameters:

32

- now (bool): Whether to restart immediately

33

- newports (bool): Whether to use new ports (ignored for in-process)

34

- **kwargs: Additional restart options

35

"""

36

37

def shutdown_kernel(self, now=False, restart=False):

38

"""

39

Shutdown the in-process kernel.

40

41

Parameters:

42

- now (bool): Whether to shutdown immediately

43

- restart (bool): Whether this is part of a restart

44

"""

45

46

def interrupt_kernel(self):

47

"""Interrupt the in-process kernel."""

48

49

def signal_kernel(self, signum):

50

"""

51

Send signal to kernel.

52

53

Parameters:

54

- signum (int): Signal number to send

55

"""

56

57

def is_alive(self):

58

"""

59

Check if kernel is alive.

60

61

Returns:

62

bool: True if kernel is running

63

"""

64

65

# Manager attributes

66

kernel: object # The in-process kernel instance

67

client_class: type # Client class for this manager

68

```

69

70

### In-Process Kernel Client

71

72

Client for interacting with in-process kernels.

73

74

```python { .api }

75

class InProcessKernelClient:

76

"""

77

Client for in-process kernel communication.

78

79

Provides methods for executing code, getting completions, and

80

other kernel interactions within the same process.

81

"""

82

83

def execute(self, code, silent=False, store_history=True,

84

user_expressions=None, allow_stdin=None, stop_on_error=True):

85

"""

86

Execute code in the in-process kernel.

87

88

Parameters:

89

- code (str): Code to execute

90

- silent (bool): Whether to suppress output

91

- store_history (bool): Whether to store in history

92

- user_expressions (dict, optional): Expressions to evaluate

93

- allow_stdin (bool, optional): Whether to allow stdin

94

- stop_on_error (bool): Whether to stop on errors

95

96

Returns:

97

str: Message ID for the execution request

98

"""

99

100

def complete(self, code, cursor_pos=None):

101

"""

102

Request code completion.

103

104

Parameters:

105

- code (str): Code context for completion

106

- cursor_pos (int, optional): Cursor position

107

108

Returns:

109

str: Message ID for the completion request

110

"""

111

112

def inspect(self, code, cursor_pos=None, detail_level=0):

113

"""

114

Request object inspection.

115

116

Parameters:

117

- code (str): Code containing object to inspect

118

- cursor_pos (int, optional): Cursor position

119

- detail_level (int): Level of detail

120

121

Returns:

122

str: Message ID for the inspection request

123

"""

124

125

def history(self, raw=True, output=False, hist_access_type='range', **kwargs):

126

"""

127

Request execution history.

128

129

Parameters:

130

- raw (bool): Whether to return raw input

131

- output (bool): Whether to include output

132

- hist_access_type (str): Type of history access

133

- **kwargs: Additional history options

134

135

Returns:

136

str: Message ID for the history request

137

"""

138

139

def kernel_info(self):

140

"""

141

Request kernel information.

142

143

Returns:

144

str: Message ID for the kernel info request

145

"""

146

147

def shutdown(self, restart=False):

148

"""

149

Request kernel shutdown.

150

151

Parameters:

152

- restart (bool): Whether this is a restart

153

154

Returns:

155

str: Message ID for the shutdown request

156

"""

157

```

158

159

### Blocking In-Process Client

160

161

Synchronous client that blocks until responses are received.

162

163

```python { .api }

164

class BlockingInProcessKernelClient:

165

"""

166

Blocking client for in-process kernels.

167

168

Provides synchronous methods that block until responses are received,

169

making it easier to use in synchronous code.

170

"""

171

172

def execute_interactive(self, code, silent=False, store_history=True,

173

user_expressions=None, allow_stdin=None,

174

timeout=None):

175

"""

176

Execute code and wait for completion.

177

178

Parameters:

179

- code (str): Code to execute

180

- silent (bool): Whether to suppress output

181

- store_history (bool): Whether to store in history

182

- user_expressions (dict, optional): Expressions to evaluate

183

- allow_stdin (bool, optional): Whether to allow stdin

184

- timeout (float, optional): Timeout in seconds

185

186

Returns:

187

dict: Execution result

188

"""

189

190

def get_shell_msg(self, block=True, timeout=None):

191

"""

192

Get message from shell channel.

193

194

Parameters:

195

- block (bool): Whether to block until message available

196

- timeout (float, optional): Timeout in seconds

197

198

Returns:

199

dict: Message from shell channel

200

"""

201

202

def get_iopub_msg(self, block=True, timeout=None):

203

"""

204

Get message from IOPub channel.

205

206

Parameters:

207

- block (bool): Whether to block until message available

208

- timeout (float, optional): Timeout in seconds

209

210

Returns:

211

dict: Message from IOPub channel

212

"""

213

```

214

215

### In-Process Channels

216

217

Communication channels for in-process kernel interaction.

218

219

```python { .api }

220

class InProcessChannel:

221

"""

222

Base channel for in-process kernel communication.

223

224

Provides the communication interface between client and kernel

225

within the same process.

226

"""

227

228

def send(self, msg):

229

"""

230

Send message through this channel.

231

232

Parameters:

233

- msg (dict): Message to send

234

"""

235

236

def get_msg(self, block=True, timeout=None):

237

"""

238

Get message from this channel.

239

240

Parameters:

241

- block (bool): Whether to block until message available

242

- timeout (float, optional): Timeout in seconds

243

244

Returns:

245

dict: Received message

246

"""

247

248

def get_msgs(self):

249

"""

250

Get all available messages.

251

252

Returns:

253

list: List of all available messages

254

"""

255

256

def msg_ready(self):

257

"""

258

Check if messages are available.

259

260

Returns:

261

bool: True if messages are ready

262

"""

263

264

class InProcessHBChannel:

265

"""

266

Heartbeat channel for in-process kernels.

267

268

Handles heartbeat/ping functionality for kernel liveness checking.

269

"""

270

271

def start(self):

272

"""Start the heartbeat channel."""

273

274

def stop(self):

275

"""Stop the heartbeat channel."""

276

277

def is_beating(self):

278

"""

279

Check if heartbeat is active.

280

281

Returns:

282

bool: True if heartbeat is active

283

"""

284

```

285

286

## Usage Examples

287

288

### Basic In-Process Kernel Usage

289

290

```python

291

from ipykernel.inprocess import InProcessKernelManager, InProcessKernelClient

292

293

# Create and start in-process kernel

294

km = InProcessKernelManager()

295

km.start_kernel()

296

297

# Create client

298

kc = InProcessKernelClient()

299

kc.load_connection_info(km.get_connection_info())

300

301

# Execute code

302

msg_id = kc.execute("x = 42; print(x)")

303

304

# Get execution results (non-blocking)

305

while True:

306

try:

307

msg = kc.get_shell_msg(timeout=1)

308

if msg['parent_header']['msg_id'] == msg_id:

309

print(f"Execution status: {msg['content']['status']}")

310

break

311

except:

312

break

313

314

# Cleanup

315

km.shutdown_kernel()

316

```

317

318

### Synchronous In-Process Operations

319

320

```python

321

from ipykernel.inprocess import InProcessKernelManager, BlockingInProcessKernelClient

322

323

# Create and start kernel

324

km = InProcessKernelManager()

325

km.start_kernel()

326

327

# Create blocking client for synchronous operations

328

kc = BlockingInProcessKernelClient()

329

kc.load_connection_info(km.get_connection_info())

330

331

# Execute code synchronously

332

result = kc.execute_interactive("import math; math.sqrt(16)")

333

print(f"Execution result: {result}")

334

335

# Get completion suggestions synchronously

336

completions = kc.complete("mat", 3)

337

print(f"Completions: {completions}")

338

339

# Get object inspection synchronously

340

inspection = kc.inspect("math.sqrt", 9)

341

print(f"Inspection: {inspection}")

342

343

# Cleanup

344

km.shutdown_kernel()

345

```

346

347

### In-Process Kernel in Class Context

348

349

```python

350

from ipykernel.inprocess import InProcessKernelManager, BlockingInProcessKernelClient

351

352

class CalculationEngine:

353

"""Example calculation engine using in-process kernel."""

354

355

def __init__(self):

356

self.km = InProcessKernelManager()

357

self.km.start_kernel()

358

359

self.kc = BlockingInProcessKernelClient()

360

self.kc.load_connection_info(self.km.get_connection_info())

361

362

# Initialize kernel with utility functions

363

self._setup_kernel()

364

365

def _setup_kernel(self):

366

"""Setup kernel with utility functions."""

367

setup_code = """

368

import math

369

import statistics

370

371

def calculate_stats(data):

372

return {

373

'mean': statistics.mean(data),

374

'median': statistics.median(data),

375

'stdev': statistics.stdev(data) if len(data) > 1 else 0

376

}

377

"""

378

self.kc.execute_interactive(setup_code)

379

380

def execute_calculation(self, code):

381

"""Execute calculation code in kernel."""

382

result = self.kc.execute_interactive(code)

383

return result

384

385

def calculate_statistics(self, data):

386

"""Calculate statistics using kernel."""

387

code = f"calculate_stats({data})"

388

result = self.kc.execute_interactive(code)

389

return result

390

391

def shutdown(self):

392

"""Cleanup kernel resources."""

393

self.km.shutdown_kernel()

394

395

# Usage

396

engine = CalculationEngine()

397

398

# Execute calculations

399

result = engine.execute_calculation("2 ** 10")

400

print(f"2^10 = {result}")

401

402

# Calculate statistics

403

stats = engine.calculate_statistics([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

404

print(f"Statistics: {stats}")

405

406

# Cleanup

407

engine.shutdown()

408

```

409

410

### Interactive Data Processing

411

412

```python

413

from ipykernel.inprocess import InProcessKernelManager, BlockingInProcessKernelClient

414

415

class DataProcessor:

416

"""Interactive data processor using in-process kernel."""

417

418

def __init__(self):

419

self.km = InProcessKernelManager()

420

self.km.start_kernel()

421

422

self.kc = BlockingInProcessKernelClient()

423

self.kc.load_connection_info(self.km.get_connection_info())

424

425

# Setup data processing environment

426

self._setup_environment()

427

428

def _setup_environment(self):

429

"""Setup kernel with data processing libraries."""

430

setup_code = """

431

import pandas as pd

432

import numpy as np

433

434

# Global data storage

435

datasets = {}

436

437

def load_dataset(name, data):

438

datasets[name] = pd.DataFrame(data)

439

return f"Loaded dataset '{name}' with {len(data)} rows"

440

441

def process_dataset(name, operation):

442

if name not in datasets:

443

return f"Dataset '{name}' not found"

444

445

df = datasets[name]

446

if operation == 'describe':

447

return df.describe().to_dict()

448

elif operation == 'head':

449

return df.head().to_dict()

450

else:

451

return f"Unknown operation: {operation}"

452

"""

453

self.kc.execute_interactive(setup_code)

454

455

def load_data(self, name, data):

456

"""Load data into kernel."""

457

code = f"load_dataset('{name}', {data})"

458

result = self.kc.execute_interactive(code)

459

return result

460

461

def process_data(self, name, operation):

462

"""Process data in kernel."""

463

code = f"process_dataset('{name}', '{operation}')"

464

result = self.kc.execute_interactive(code)

465

return result

466

467

def execute_custom(self, code):

468

"""Execute custom data processing code."""

469

return self.kc.execute_interactive(code)

470

471

def shutdown(self):

472

"""Cleanup resources."""

473

self.km.shutdown_kernel()

474

475

# Usage

476

processor = DataProcessor()

477

478

# Load sample data

479

data = [

480

{'name': 'Alice', 'age': 25, 'salary': 50000},

481

{'name': 'Bob', 'age': 30, 'salary': 60000},

482

{'name': 'Charlie', 'age': 35, 'salary': 70000}

483

]

484

485

processor.load_data('employees', data)

486

487

# Process data

488

stats = processor.process_data('employees', 'describe')

489

print(f"Employee statistics: {stats}")

490

491

# Custom processing

492

result = processor.execute_custom("""

493

df = datasets['employees']

494

high_earners = df[df['salary'] > 55000]

495

high_earners.to_dict('records')

496

""")

497

print(f"High earners: {result}")

498

499

# Cleanup

500

processor.shutdown()

501

```