Guest User

Untitled

a guest
Mar 8th, 2014
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. proc.send_signal(signal.SIGINT)
  2.  
  3. proc.send_signal(signal.CTRL_C_EVENT)
  4.  
  5. # Windows needs an extra argument passed to subprocess.Popen,
  6. # but the constant isn't defined on Unix.
  7. try: kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
  8. except AttributeError: pass
  9. proc = subprocess.Popen(argv,
  10. stdin=open(os.path.devnull, "r"),
  11. stdout=subprocess.PIPE,
  12. stderr=subprocess.PIPE,
  13. **kwargs)
  14.  
  15. import ctypes
  16.  
  17. ctypes.windll.kernel32.GenerateConsoleCtrlEvent(0, proc.pid) # 0 => Ctrl-C
  18.  
  19. #wrapper.py
  20. import subprocess, time, signal, sys, os
  21.  
  22. def signal_handler(signal, frame):
  23. time.sleep(1)
  24. print 'Ctrl+C received in wrapper.py'
  25.  
  26. signal.signal(signal.SIGINT, signal_handler)
  27. print "wrapper.py started"
  28. subprocess.Popen("python demo.py")
  29. time.sleep(3) #Replace with your IPC code here, which waits on a fire CTRL-C request
  30. os.kill(signal.CTRL_C_EVENT, 0)
  31.  
  32. #demo.py
  33.  
  34. import signal, sys, time
  35.  
  36. def signal_handler(signal, frame):
  37. print 'Ctrl+C received in demo.py'
  38. time.sleep(1)
  39. sys.exit(0)
  40.  
  41. signal.signal(signal.SIGINT, signal_handler)
  42. print 'demo.py started'
  43. #signal.pause() # does not work under Windows
  44. while(True):
  45. time.sleep(1)
  46.  
  47. PythonPrompt> import subprocess
  48. PythonPrompt> subprocess.Popen("start python wrapper.py", shell=True)
  49.  
  50. >>> import winctrlc
  51. >>> p1 = winctrlc.Popen("python demo.py")
  52. >>> p2 = winctrlc.Popen("python demo.py")
  53. >>> p3 = winctrlc.Popen("python demo.py")
  54. >>> p2.send_ctrl_c()
  55. >>> p1.send_ctrl_c()
  56. >>> p3.send_ctrl_c()
  57.  
  58. import socket
  59. import subprocess
  60. import time
  61. import random
  62. import signal, os, sys
  63.  
  64.  
  65. class Popen:
  66. _port = random.randint(10000, 50000)
  67. _connection = ''
  68.  
  69. def _start_ctrl_c_wrapper(self, cmd):
  70. cmd_str = "start "" python winctrlc.py "+"""+cmd+"""+" "+str(self._port)
  71. subprocess.Popen(cmd_str, shell=True)
  72.  
  73. def _create_connection(self):
  74. self._connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  75. self._connection.connect(('localhost', self._port))
  76.  
  77. def send_ctrl_c(self):
  78. self._connection.send(Wrapper.TERMINATION_REQ)
  79. self._connection.close()
  80.  
  81. def __init__(self, cmd):
  82. self._start_ctrl_c_wrapper(cmd)
  83. self._create_connection()
  84.  
  85.  
  86. class Wrapper:
  87. TERMINATION_REQ = "Terminate with CTRL-C"
  88.  
  89. def _create_connection(self, port):
  90. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  91. s.bind(('localhost', port))
  92. s.listen(1)
  93. conn, addr = s.accept()
  94. return conn
  95.  
  96. def _wait_on_ctrl_c_request(self, conn):
  97. while True:
  98. data = conn.recv(1024)
  99. if data == self.TERMINATION_REQ:
  100. ctrl_c_received = True
  101. break
  102. else:
  103. ctrl_c_received = False
  104. return ctrl_c_received
  105.  
  106. def _cleanup_and_fire_ctrl_c(self, conn):
  107. conn.close()
  108. os.kill(signal.CTRL_C_EVENT, 0)
  109.  
  110. def _signal_handler(self, signal, frame):
  111. time.sleep(1)
  112. sys.exit(0)
  113.  
  114. def __init__(self, cmd, port):
  115. signal.signal(signal.SIGINT, self._signal_handler)
  116. subprocess.Popen(cmd)
  117. conn = self._create_connection(port)
  118. ctrl_c_req_received = self._wait_on_ctrl_c_request(conn)
  119. if ctrl_c_req_received:
  120. self._cleanup_and_fire_ctrl_c(conn)
  121. else:
  122. sys.exit(0)
  123.  
  124.  
  125. if __name__ == "__main__":
  126. command_string = sys.argv[1]
  127. port_no = int(sys.argv[2])
  128. Wrapper(command_string, port_no)
  129.  
  130. import subprocess, time, signal, sys, os
  131. command = '"C:\Program Files\fio\fio.exe" --rw=randrw --bs=1M --numjobs=8 --iodepth=64 --direct=1 '
  132. '--sync=0 --ioengine=windowsaio --name=test --loops=10000 '
  133. '--size=99901800 --rwmixwrite=100 --do_verify=0 --filename=I\:\test '
  134. '--thread --output=C:\output.txt'
  135. def signal_handler(signal, frame):
  136. time.sleep(1)
  137. print 'Ctrl+C received in wrapper.py'
  138.  
  139. signal.signal(signal.SIGINT, signal_handler)
  140. print 'command Starting'
  141. subprocess.Popen(command)
  142. print 'command started'
  143. time.sleep(15)
  144. print 'Timeout Completed'
  145. os.kill(signal.CTRL_C_EVENT, 0)
RAW Paste Data