or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

core-concurrency.mddebugging.mdgreen-stdlib.mdindex.mdmonkey-patching.mdnetworking.mdresource-pooling.mdsynchronization.mdthread-pools.mdweb-server.md

index.mddocs/

0

# Eventlet

1

2

A concurrent networking library for Python that enables developers to write scalable network applications using coroutines and non-blocking I/O. Eventlet provides a blocking programming style similar to threading while delivering the performance benefits of asynchronous I/O through epoll or libevent.

3

4

## Package Information

5

6

- **Package Name**: eventlet

7

- **Language**: Python

8

- **Installation**: `pip install eventlet`

9

10

## Core Imports

11

12

```python

13

import eventlet

14

```

15

16

Common imports for networking and concurrency:

17

18

```python

19

from eventlet import GreenPool, spawn, sleep

20

from eventlet.green import socket, ssl

21

import eventlet.wsgi

22

```

23

24

## Basic Usage

25

26

```python

27

import eventlet

28

29

# Enable monkey patching for cooperative networking

30

eventlet.monkey_patch()

31

32

def handle_client(sock, addr):

33

"""Handle a client connection"""

34

try:

35

data = sock.recv(1024)

36

sock.send(b"Echo: " + data)

37

finally:

38

sock.close()

39

40

def server():

41

"""Simple echo server"""

42

server_sock = eventlet.listen(('localhost', 8080))

43

print("Server listening on localhost:8080")

44

45

pool = eventlet.GreenPool(1000) # Max 1000 concurrent connections

46

47

try:

48

while True:

49

client_sock, addr = server_sock.accept()

50

pool.spawn(handle_client, client_sock, addr)

51

except KeyboardInterrupt:

52

print("Server shutting down")

53

finally:

54

server_sock.close()

55

56

if __name__ == "__main__":

57

server()

58

```

59

60

## Architecture

61

62

Eventlet is built around several core concepts:

63

64

- **Green Threads**: Lightweight coroutines that yield control cooperatively

65

- **Event Loop (Hub)**: Central dispatcher managing I/O events and scheduling

66

- **Monkey Patching**: Transparent replacement of standard library modules with green versions

67

- **Green Standard Library**: Cooperative versions of Python standard library modules

68

- **Pools**: Resource management for controlling concurrency levels

69

70

This design enables high-concurrency applications while maintaining familiar blocking programming patterns, making it easy to integrate into existing applications without requiring significant code restructuring.

71

72

## Capabilities

73

74

### Core Concurrency

75

76

Essential green threading primitives for managing cooperative concurrency, including spawning greenthreads, controlling execution flow, and managing lifecycle.

77

78

```python { .api }

79

def spawn(func, *args, **kwargs): ...

80

def spawn_n(func, *args, **kwargs): ...

81

def spawn_after(seconds, func, *args, **kwargs): ...

82

def spawn_after_local(seconds, func, *args, **kwargs): ...

83

def sleep(seconds=0): ...

84

def kill(gt, exc_type=None): ...

85

def getcurrent(): ...

86

```

87

88

[Core Concurrency](./core-concurrency.md)

89

90

### Synchronization Primitives

91

92

Thread-safe synchronization tools including events, semaphores, queues, and timeouts for coordinating between greenthreads.

93

94

```python { .api }

95

class Event:

96

def send(self, result=None): ...

97

def wait(self, timeout=None): ...

98

def ready(self): ...

99

def has_exception(self): ...

100

def has_result(self): ...

101

def poll(self, notready=None): ...

102

def send_exception(self, *args): ...

103

104

class Semaphore:

105

def acquire(self, blocking=True): ...

106

def release(self): ...

107

108

class Queue:

109

def put(self, item, block=True, timeout=None): ...

110

def get(self, block=True, timeout=None): ...

111

```

112

113

[Synchronization](./synchronization.md)

114

115

### Green Thread Pools

116

117

Managed pools of green threads for controlling concurrency levels and resource usage in high-throughput applications.

118

119

```python { .api }

120

class GreenPool:

121

def __init__(self, size=1000): ...

122

def spawn(self, func, *args, **kwargs): ...

123

def spawn_n(self, func, *args, **kwargs): ...

124

def waitall(self): ...

125

def resize(self, new_size): ...

126

def imap(self, func, *iterables): ...

127

def starmap(self, func, iterable): ...

128

129

class GreenPile:

130

def spawn(self, func, *args, **kwargs): ...

131

def __iter__(self): ...

132

def next(self): ...

133

```

134

135

[Thread Pools](./thread-pools.md)

136

137

### Networking

138

139

High-level networking functions and utilities for creating client and server applications with SSL/TLS support.

140

141

```python { .api }

142

def connect(addr, family=socket.AF_INET, bind=None): ...

143

def listen(addr, family=socket.AF_INET, backlog=50, reuse_addr=True, reuse_port=None): ...

144

def serve(sock, handle, concurrency=1000): ...

145

def wrap_ssl(sock, *args, **kw): ...

146

```

147

148

[Networking](./networking.md)

149

150

### Web Server (WSGI)

151

152

Production-ready WSGI server implementation for hosting web applications with built-in support for WebSocket upgrades.

153

154

```python { .api }

155

def server(sock, site, log=None, environ=None, max_size=None,

156

max_http_version=None, protocol=wsgi.HttpProtocol,

157

server_event=None, minimum_chunk_size=None,

158

log_x_forwarded_for=True, custom_pool=None,

159

keepalive=True, log_output=True, log_format=None,

160

url_length_limit=8192, debug=True, socket_timeout=60,

161

capitalize_response_headers=True): ...

162

```

163

164

[Web Server](./web-server.md)

165

166

### Monkey Patching

167

168

System for transparently replacing standard library modules with green cooperative versions.

169

170

```python { .api }

171

def monkey_patch(os=True, select=True, socket=True, thread=True,

172

time=True, ssl=True, httplib=False,

173

subprocess=False, all=None, profile=False,

174

aggressive=True): ...

175

def import_patched(modulename, *additional_modules, **kw_additional_modules): ...

176

def is_monkey_patched(module): ...

177

```

178

179

[Monkey Patching](./monkey-patching.md)

180

181

### Green Standard Library

182

183

Cooperative versions of Python standard library modules that work seamlessly with eventlet's green threading model.

184

185

```python { .api }

186

# Available green modules

187

import eventlet.green.socket

188

import eventlet.green.ssl

189

import eventlet.green.urllib.request

190

import eventlet.green.http.client

191

import eventlet.green.threading

192

import eventlet.green.subprocess

193

# ... and many more

194

```

195

196

[Green Standard Library](./green-stdlib.md)

197

198

### Resource Pooling

199

200

Generic resource pooling for managing expensive resources like database connections with automatic lifecycle management.

201

202

```python { .api }

203

class Pool:

204

def __init__(self, min_size=0, max_size=4, order_as_stack=False, create=None): ...

205

def get(self): ...

206

def put(self, item): ...

207

def item(self): ...

208

209

class TokenPool(Pool):

210

def __init__(self, max_size=4): ...

211

def create(self): ...

212

213

class ConnectionPool:

214

def __init__(self, db_module, *args, **kwargs): ...

215

def get(self): ...

216

def put(self, conn): ...

217

```

218

219

[Resource Pooling](./resource-pooling.md)

220

221

### Debugging and Utilities

222

223

Tools for debugging, introspection, and monitoring eventlet applications including tracing, hub inspection, and performance analysis.

224

225

```python { .api }

226

def spew(): ...

227

def unspew(): ...

228

def format_hub_listeners(): ...

229

def format_hub_timers(): ...

230

def hub_blocking_detection(state): ...

231

```

232

233

[Debugging](./debugging.md)

234

235

## Types

236

237

```python { .api }

238

class GreenThread:

239

"""A greenlet subclass that can be used to retrieve return values."""

240

def wait(self): ...

241

def kill(self, exc_type=None): ...

242

def link(self, func, *curried_args, **curried_kwargs): ...

243

def unlink(self, func, *curried_args, **curried_kwargs): ...

244

245

class StopServe(Exception):

246

"""Exception class used to gracefully stop a serve() loop."""

247

pass

248

249

class Timeout(Exception):

250

"""A timeout context manager and exception."""

251

def __init__(self, seconds=None, exception=None): ...

252

def __enter__(self): ...

253

def __exit__(self, typ, value, tb): ...

254

```