def __init__(self, filename=None, mode=None, compresslevel=9, fileobj=None): """ Wrapper with locking for constructor for the GzipFile class. At least one of fileobj and filename must be given a non-trivial value. The new class instance is based on fileobj, which can be a regular file, a StringIO object, or any other object which simulates a file. It defaults to None, in which case filename is opened to provide a file object. When fileobj is not None, the filename argument is only used to be included in the gzip file header, which may includes the original filename of the uncompressed file. It defaults to the filename of fileobj, if discernible; otherwise, it defaults to the empty string, and in this case the original filename is not included in the header. The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb', depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is 'rb'. Be aware that only the 'rb', 'ab', and 'wb' values should be used for cross-platform portability. The compresslevel argument is an integer from 1 to 9 controlling the level of compression; 1 is fastest and produces the least compression, and 9 is slowest and produces the most compression. The default is 9. """ gzip.GzipFile.__init__(self, filename, mode, compresslevel, fileobj) self.lock = _Semaphore() self.__size = None
class DebugSemaphore(threading._Semaphore): """ threading.Semaphore like class with helper for fighting dead-locks """ write_lock = threading._Semaphore() blocked = [] def __init__(self, *arg, **kwarg): threading._Semaphore.__init__(self, *arg, **kwarg) def acquire(self, *arg, **kwarg): if self._Semaphore__value == 0: with self.write_lock: self.blocked.append(id(self)) sys.stderr.write(os.linesep.join(["Blocking sem %s" % id(self)] + \ traceback.format_stack()[:-1] + [""])) return threading._Semaphore.acquire(self, *arg, **kwarg) def release(self, *arg, **kwarg): with self.write_lock: uid = id(self) if uid in self.blocked: self.blocked.remove(uid) sys.stderr.write("Released sem %s %s" % (uid, os.linesep)) threading._Semaphore.release(self, *arg, **kwarg) def __enter__(self): self.acquire() return self def __exit__(self, *arg, **kwarg): self.release()
def __init__(self, name, mode="rb", buffering=0, temporary=False): """file(name[, mode[, buffering]]) -> file object Open a file. The mode can be 'r', 'w' or 'a' for reading (default), writing or appending. The file will be created if it doesn't exist when opened for writing or appending; it will be truncated when opened for writing. Add a 'b' to the mode for binary files. Add a '+' to the mode to allow simultaneous reading and writing. If the buffering argument is given, 0 means unbuffered, 1 means line buffered, and larger numbers specify the buffer size. The preferred way to open a file is with the builtin open() function. Add a 'U' to mode to open the file for input with universal newline support. Any line ending in the input file will be seen as a '\n' in Python. Also, a file so opened gains the attribute 'newlines'; the value for this attribute is one of None (no newline read yet), '\r', '\n', '\r\n' or a tuple containing all the newline types seen. 'U' cannot be combined with 'w' or '+' mode. :param temporary: if True, destroy file at close. """ if six.PY2: FileIO.__init__(self, name, mode, buffering) else: # for python3 we drop buffering FileIO.__init__(self, name, mode) self.lock = _Semaphore() self.__size = None self.__temporary = temporary
def __init__(self, name, mode="rb", buffering=0): """file(name[, mode[, buffering]]) -> file object Open a file. The mode can be 'r', 'w' or 'a' for reading (default), writing or appending. The file will be created if it doesn't exist when opened for writing or appending; it will be truncated when opened for writing. Add a 'b' to the mode for binary files. Add a '+' to the mode to allow simultaneous reading and writing. If the buffering argument is given, 0 means unbuffered, 1 means line buffered, and larger numbers specify the buffer size. The preferred way to open a file is with the builtin open() function. Add a 'U' to mode to open the file for input with universal newline support. Any line ending in the input file will be seen as a '\n' in Python. Also, a file so opened gains the attribute 'newlines'; the value for this attribute is one of None (no newline read yet), '\r', '\n', '\r\n' or a tuple containing all the newline types seen. 'U' cannot be combined with 'w' or '+' mode. """ if six.PY2: FileIO.__init__(self, name, mode, buffering) else: # for python3 we drop buffering FileIO.__init__(self, name, mode) self.lock = _Semaphore() self.__size = None
def __init__(self, data, fname=None, mode="r"): six.BytesIO.__init__(self, data) if "closed" not in dir(self): self.closed = False if fname is None: self.name = "fabioStream" else: self.name = fname self.mode = mode self.lock = _Semaphore() self.__size = None
def __init__(self, data, fname=None, mode="r"): six.BytesIO.__init__(self, data) if not "closed" in dir(self): self.closed = False if fname == None: self.name = "fabioStream" else: self.name = fname self.mode = mode self.lock = _Semaphore() self.__size = None
def __init__(self, name, mode='r', buffering=0, compresslevel=9): """ BZ2File(name [, mode='r', compresslevel=9]) -> file object Open a bz2 file. The mode can be 'r' or 'w', for reading (default) or writing. When opened for writing, the file will be created if it doesn't exist, and truncated otherwise. If compresslevel is given, must be a number between 1 and 9. Add a 'U' to mode to open the file for input with universal newline support. Any line ending in the input file will be seen as a '\n' in Python. Also, a file so opened gains the attribute 'newlines'; the value for this attribute is one of None (no newline read yet), '\r', '\n', '\r\n' or a tuple containing all the newline types seen. Universal newlines are available only when reading. """ bz2.BZ2File.__init__(self, name, mode, buffering, compresslevel) self.lock = _Semaphore() self.__size = None
def __init__(self, name , mode='r', buffering=0, compresslevel=9): """ BZ2File(name [, mode='r', buffering=0, compresslevel=9]) -> file object Open a bz2 file. The mode can be 'r' or 'w', for reading (default) or writing. When opened for writing, the file will be created if it doesn't exist, and truncated otherwise. If the buffering argument is given, 0 means unbuffered, and larger numbers specify the buffer size. If compresslevel is given, must be a number between 1 and 9. Add a 'U' to mode to open the file for input with universal newline support. Any line ending in the input file will be seen as a '\n' in Python. Also, a file so opened gains the attribute 'newlines'; the value for this attribute is one of None (no newline read yet), '\r', '\n', '\r\n' or a tuple containing all the newline types seen. Universal newlines are available only when reading. """ bz2.BZ2File.__init__(self, name , mode, buffering, compresslevel) self.lock = _Semaphore() self.__size = None
from bottle import route, run, get, post, response, static_file, request import cv2 import urllib import numpy as np import time import threading from controlCommand import * import os sem = threading._Semaphore() stream = urllib.urlopen('http://localhost:8080/stream?topic=/image_view/output') bytes = bytes() rdbuffer = np.array([]) def thread_inc(): global stream, rdbuffer, bytes print "aaa" while True: bytes += stream.read(1024) a = bytes.find(b'\xff\xd8') b = bytes.find(b'\xff\xd9') if a != -1 and b != -1: sem.acquire() rdbuffer = np.copy(bytes[a:b+2]) bytes = bytes[b+2:] sem.release() time.sleep(0.001)### def forceToKill(): appName = 'python' killApp = 'killall -9 ' + appName
from threading import Thread, _Semaphore from random import * from time import sleep verrou=_Semaphore(2) class Document(Thread): ID = 1 def __init__(self): Thread.__init__(self) self.ID = Document.ID self.page = randint(1, 10) Document.ID += 1 def run(self): verrou.acquire() for i in range(1, self.page): sleep(0.5) print('Thread #{} : Page #{}'.format(self.ID, i)) verrou.release() for i in range(10): Document().start()