Skip to content

Instantly share code, notes, and snippets.

@jtzero
Created November 13, 2013 21:22
Show Gist options
  • Save jtzero/7456626 to your computer and use it in GitHub Desktop.
Save jtzero/7456626 to your computer and use it in GitHub Desktop.
@metajack's post saved from codexon Category: Internet, Programming, Python The recent server benchmark posted here comparing Erlang, Haskell, and Python understandably upset many people. Today we will move away from polarizing benchmarks and more about features. Even though Erlang may not be the fastest language, it still has at least one grea…
import os
import time
import socket
import sys
import fcntl
import _multiprocessing
from multiprocessing import Pipe
# global pipe variable
pipe = None
spare_pipe = None
def PipeFromFD(fd):
return _multiprocessing.Connection(fd)
def recordPipe(p1, p2):
with open('fd.txt', 'w') as f:
f.write('%s,%s' % (p1.fileno(), p2.fileno()))
def upgrade():
os.spawnlp(os.P_NOWAIT, 'python', 'python', sys.argv[0], '-upgrade')
def upgradeBootstrap():
global pipe, spare_pipe
with open('fd.txt', 'r') as f:
p1, p2 = (int(fd) for fd in f.read().split(','))
pipe = PipeFromFD(p1)
spare_pipe = PipeFromFD(p2)
recordPipe(spare_pipe, pipe)
pipe.send("I am upgraded!")
if __name__ == '__main__':
if '-upgrade' in sys.argv:
upgradeBootstrap()
else:
pipe, spare_pipe = Pipe()
recordPipe(spare_pipe, pipe)
pid = os.getpid()
print '%i: Version 1. Upgrade in 5 seconds.' % pid
time.sleep(5)
print '%i: Upgrading' % pid
upgrade()
print '%i: Message from new process: %s' % (pid, pipe.recv())
print '%i: Upgrade Complete. Exiting' % pid
Example Output
user@test:/mnt/shared$ python upgrade.py
22930: Version 1. Upgrade in 10 seconds.
22930: Upgrading
22971: Version 2. Upgrade in 10 seconds.
22930: Message from new process: I am upgraded!
22930: Upgrade Complete. Exiting
user@test:/mnt/shared$ 22971: Upgrading
23025: Version 3. Upgrade in 10 seconds.
22971: Message from new process: I am upgraded!
22971: Upgrade Complete. Exiting
killall python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment