Skip to content

Instantly share code, notes, and snippets.

@norbe
Created July 25, 2014 07:38
Show Gist options
  • Save norbe/e36f994914da5e2bb43e to your computer and use it in GitHub Desktop.
Save norbe/e36f994914da5e2bb43e to your computer and use it in GitHub Desktop.
spawn-php windows
# -*- coding: utf-8 -*-
#
# spawn-php.py
# This is a program to spawn php-cgi on windows.
#
# Author: [email protected]
#
# Version 1.0
#
import sys
import os
import win32process
import win32api
import subprocess
import shlex
import time
import signal
DEFAULT_STARTPORT = 9000
PROCS_LIMIT = 10
BINDS_TO = "127.0.0.1"
PHP_CMD = "php-cgi.exe"
PROCESS_TERMINATE = 1
startport = DEFAULT_STARTPORT
numprocs = PROCS_LIMIT
pids = dict()
for i in range(numprocs):
myport = startport + i
cmd_line = '{0} -b {1}:{2}'.format(PHP_CMD, BINDS_TO, myport)
args = shlex.split(cmd_line)
process = subprocess.Popen(args)
pids[myport] = process.pid
print 'spawning php on port {0}...success! pid: {1}'.format(myport, process.pid)
def signal_handler(signal, frame):
print "SIGINT received!"
for myport,pid in pids.iteritems():
if win32process.GetProcessVersion(pid) > 1:
handle = win32api.OpenProcess(PROCESS_TERMINATE, False, pid)
win32api.TerminateProcess(handle, -1)
win32api.CloseHandle(handle)
print "php on port {0}(pid {1}) stopped.".format(myport, pid)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
count = 0
while 1:
time.sleep(0.1)
for myport,pid in pids.iteritems():
if win32process.GetProcessVersion(pid) < 1:
cmd_line = '{0} -b {1}:{2}'.format(PHP_CMD, BINDS_TO, myport)
args = shlex.split(cmd_line)
process = subprocess.Popen(args)
pids[myport] = process.pid
count = count + 1
print '[{3}] php running on port {0}(pid {1}) exited, restarting...new pid {2}'.format(myport, pid, process.pid, count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment