BVB Source Codes

DeDRM_tools Show subasyncio.py Source code

Return Download DeDRM_tools: download subasyncio.py Source code - Download DeDRM_tools Source code - Type:.py
  1. #!/usr/bin/env python
  2. # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
  3.  
  4. import os, sys
  5. import signal
  6. import threading
  7. import subprocess
  8. from subprocess import Popen, PIPE, STDOUT
  9.  
  10. # **heavily** chopped up and modfied version of asyncproc.py
  11. # to make it actually work on Windows as well as Mac/Linux
  12. # For the original see:
  13. # "http://www.lysator.liu.se/~bellman/download/"
  14. # author is  "Thomas Bellman <bellman@lysator.liu.se>"
  15. # available under GPL version 3 or Later
  16.  
  17. # create an asynchronous subprocess whose output can be collected in
  18. # a non-blocking manner
  19.  
  20. # What a mess!  Have to use threads just to get non-blocking io
  21. # in a cross-platform manner
  22.  
  23. # luckily all thread use is hidden within this class
  24.  
  25. class Process(object):
  26.     def __init__(self, *params, **kwparams):
  27.         if len(params) <= 3:
  28.             kwparams.setdefault('stdin', subprocess.PIPE)
  29.         if len(params) <= 4:
  30.             kwparams.setdefault('stdout', subprocess.PIPE)
  31.         if len(params) <= 5:
  32.             kwparams.setdefault('stderr', subprocess.PIPE)
  33.         self.__pending_input = []
  34.         self.__collected_outdata = []
  35.         self.__collected_errdata = []
  36.         self.__exitstatus = None
  37.         self.__lock = threading.Lock()
  38.         self.__inputsem = threading.Semaphore(0)
  39.         self.__quit = False
  40.  
  41.         self.__process = subprocess.Popen(*params, **kwparams)
  42.  
  43.         if self.__process.stdin:
  44.             self.__stdin_thread = threading.Thread(
  45.                 name="stdin-thread",
  46.                 target=self.__feeder, args=(self.__pending_input,
  47.                                             self.__process.stdin))
  48.             self.__stdin_thread.setDaemon(True)
  49.             self.__stdin_thread.start()
  50.  
  51.         if self.__process.stdout:
  52.             self.__stdout_thread = threading.Thread(
  53.                 name="stdout-thread",
  54.                 target=self.__reader, args=(self.__collected_outdata,
  55.                                             self.__process.stdout))
  56.             self.__stdout_thread.setDaemon(True)
  57.             self.__stdout_thread.start()
  58.  
  59.         if self.__process.stderr:
  60.             self.__stderr_thread = threading.Thread(
  61.                 name="stderr-thread",
  62.                 target=self.__reader, args=(self.__collected_errdata,
  63.                                             self.__process.stderr))
  64.             self.__stderr_thread.setDaemon(True)
  65.             self.__stderr_thread.start()
  66.  
  67.     def pid(self):
  68.         return self.__process.pid
  69.  
  70.     def kill(self, signal):
  71.         self.__process.send_signal(signal)
  72.  
  73.     # check on subprocess (pass in 'nowait') to act like poll
  74.     def wait(self, flag):
  75.         if flag.lower() == 'nowait':
  76.             rc = self.__process.poll()
  77.         else:
  78.             rc = self.__process.wait()
  79.         if rc != None:
  80.             if self.__process.stdin:
  81.                 self.closeinput()
  82.             if self.__process.stdout:
  83.                 self.__stdout_thread.join()
  84.             if self.__process.stderr:
  85.                 self.__stderr_thread.join()
  86.         return self.__process.returncode
  87.  
  88.     def terminate(self):
  89.         if self.__process.stdin:
  90.             self.closeinput()
  91.         self.__process.terminate()
  92.  
  93.     # thread gets data from subprocess stdout
  94.     def __reader(self, collector, source):
  95.         while True:
  96.             data = os.read(source.fileno(), 65536)
  97.             self.__lock.acquire()
  98.             collector.append(data)
  99.             self.__lock.release()
  100.             if data == "":
  101.                 source.close()
  102.                 break
  103.         return
  104.  
  105.     # thread feeds data to subprocess stdin
  106.     def __feeder(self, pending, drain):
  107.         while True:
  108.             self.__inputsem.acquire()
  109.             self.__lock.acquire()
  110.             if not pending  and self.__quit:
  111.                 drain.close()
  112.                 self.__lock.release()
  113.                 break
  114.             data = pending.pop(0)
  115.             self.__lock.release()
  116.             drain.write(data)
  117.  
  118.     # non-blocking read of data from subprocess stdout
  119.     def read(self):
  120.         self.__lock.acquire()
  121.         outdata = "".join(self.__collected_outdata)
  122.         del self.__collected_outdata[:]
  123.         self.__lock.release()
  124.         return outdata
  125.  
  126.     # non-blocking read of data from subprocess stderr
  127.     def readerr(self):
  128.         self.__lock.acquire()
  129.         errdata = "".join(self.__collected_errdata)
  130.         del self.__collected_errdata[:]
  131.         self.__lock.release()
  132.         return errdata
  133.  
  134.     # non-blocking write to stdin of subprocess
  135.     def write(self, data):
  136.         if self.__process.stdin is None:
  137.             raise ValueError("Writing to process with stdin not a pipe")
  138.         self.__lock.acquire()
  139.         self.__pending_input.append(data)
  140.         self.__inputsem.release()
  141.         self.__lock.release()
  142.  
  143.     # close stdinput of subprocess
  144.     def closeinput(self):
  145.         self.__lock.acquire()
  146.         self.__quit = True
  147.         self.__inputsem.release()
  148.         self.__lock.release()
  149.  
downloadsubasyncio.py Source code - Download DeDRM_tools Source code
Related Source Codes/Software:
kcp - KCP - A Fast and Reliable ARQ Protocol 2017-04-08
cloc - cloc counts blank lines, comment lines, and physic... 2017-04-08
nvidia-docker - Build and run Docker containers leveraging NVIDIA ... 2017-04-08
quickstart - Angular 2 QuickStart - source from the documentati... 2017-04-08
jQuery-Autocomplete - Ajax Autocomplete for jQuery allows you to easily ... 2017-04-08
fuckitjs - The Original Javascript Error Steamroller 2017-04-09
eslint-plugin-react - React specific linting rules for ESLint 2017-04-09
husky - 2017-04-09
electron-builder - A complete solution to package and build a ready f... 2017-04-09
jsfeat - JavaScript Computer Vision library. 2017-04-16
flakes - Flakes is an Admin Template Framework. A combinati... 2017-04-15
capstone - Capstone disassembly/disassembler framework: Core ... 2017-04-15
nginx-resources - A collection of resources covering Nginx, Nginx + ... 2017-04-15
utron - A lightweight MVC framework for Go(Golang) 2017-04-15
cfssl - CFSSL: Cloudflare's PKI and TLS toolkit ... 2017-04-15
HandBrake - HandBrake's main development repository ... 2017-04-15
Awesome-Networking - A curated list of awesome networking libraries, re... 2017-04-15
react-sortable-hoc - A set of higher-order components to turn any list ... 2017-04-15
jugglingdb - Multi-database ORM for nodejs: redis, mongodb, mys... 2017-04-15

 Back to top