Skip to content

Commit dfaebef

Browse files
authored
add sharing state between processes example (#390)
* clean up * add sharing state between process example * clean up
1 parent 6268c25 commit dfaebef

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

deployment/example_app/entrypoint.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11

2-
UID="$(id -u app 2>/dev/null)"
2+
USER=app
3+
UID="$(id -u "$USER" 2>/dev/null)"
34

45
if [ -z "$UID" ]; then
56
UID=0
67
USER=root
7-
else
8-
USER=app
98
fi
109

1110
if [ "$UID" -lt 10000 ]; then

examples/multiprocess_dict.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: MIT
3+
# Author: Anggit Arfanto
4+
# Description: Sharing state between processes.
5+
6+
import multiprocessing as mp
7+
8+
from tremolo import Application
9+
10+
app = Application()
11+
12+
13+
@app.route('/')
14+
def hello(request, response, **server):
15+
g = server['globals']
16+
mystate = g.options['mystate']
17+
process = mp.current_process()
18+
19+
if process.name not in mystate:
20+
mystate[process.name] = None
21+
return f'Detected {process.name}'
22+
23+
return f'Hello from: {process.name}. Workers detected: {len(mystate)}'
24+
25+
26+
if __name__ == '__main__':
27+
with mp.Manager() as manager:
28+
mystate = manager.dict()
29+
30+
# `app.run()` accepts extra/arbitrary options
31+
app.run('0.0.0.0', 8000, debug=True, worker_num=4, keepalive_timeout=1,
32+
mystate=mystate)

hello.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@app.route('/hello')
99
async def hello_world(**server):
10-
return 'Hello world!', 'latin-1'
10+
return 'Hello World!', 'latin-1'
1111

1212

1313
if __name__ == '__main__':

tremolo/tremolo.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ def create_sock(self, host, port, reuse_port=True):
565565
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
566566

567567
sock.setblocking(False)
568-
sock.set_inheritable(True)
569568

570569
if sock.family.name == 'AF_UNIX':
571570
if not host.endswith('.sock'):

0 commit comments

Comments
 (0)