def __init__(self, data=(), source=""): self.password = None if data: self.username, self.password = data self.friends = {} self.auth = None self.token = None self.lastMsgID = 0 self.rosterSet = None self.existsInDB = None self.last_udate = time.time() self.typing = {} self.source = source self.resources = [] self.chatUsers = {} self.__sync = threading._allocate_lock() self.vk = VKLogin(self.username, self.password, self.source) logger.debug("initializing User for %s" % self.source) with Database(DatabaseFile, Semaphore) as db: db("select * from users where jid=?", (self.source,)) desc = db.fetchone() if desc: if not self.token or not self.password: logger.debug("User: %s exists in db. Use it." % self.source) self.existsInDB = True self.source, self.username, self.token, self.lastMsgID, self.rosterSet = desc elif self.password or self.token: logger.debug("User: %s exists in db. Record would be deleted." % self.source) threadRun(deleteUser, (self,))
def multi_tread_run(self): input_thread = threading.Thread(target=self.input_handler, args=()) input_thread.daemon = True motor_thread = threading.Thread(target=self.motor_handler, args=()) motor_thread.daemon = True obstacle_thread = threading.Thread(target=self.obstacle_handler, args=()) obstacle_thread.daemon = True self.command = None self.command_lock = threading._allocate_lock() self.stdscr = curses.initscr() self.stdscr.nodelay(1) input_thread.start() obstacle_thread.start() motor_thread.start() while True: try: if self.command == 'quit': break except KeyboardInterrupt: self.curses.endwin() print("### End ###") break
def acquire(self, shared=False, blocking=True, timeout=None): me = current_thread() with self._lock: if self._acquirable(me, shared): self._acquire_stack.appendleft((me, shared)) return True if not blocking: return False waiter = _allocate_lock() wait_queue_entry = ((me, shared), waiter) self._wait_queue.append(wait_queue_entry) waiter.acquire() self._lock.release() if timeout is None: waiter.acquire() self._lock.acquire() self._acquire_stack.appendleft((me, shared)) self._notify() return True elif waiter.acquire(True, timeout): self._lock.acquire() self._acquire_stack.appendleft((me, shared)) self._notify() return True else: self._lock.acquire() try: self._wait_queue.remove(wait_queue_entry) except ValueError: pass self._notify() return False
def __init__(self): self._scene_loaded = False self._objects = {} self._materials = {} self.buffer = [] # cmd buffer self.callbacks = [ self.update_view, self.update_selected, self.update_materials ] ## launch Tundra ## if sys.platform == 'linux2': exe = os.path.join( CONFIG_TUNDRA, 'run-server.sh' ) assert os.path.isfile( exe ) cmd = [exe, '--config', TUNDRA_CONFIG_XML_PATH, '--fpslimit', '100', '--storage', '/tmp/'] print( cmd ) p = subprocess.Popen(cmd, stdin=subprocess.PIPE) else: exe = os.path.join( CONFIG_TUNDRA, 'Tundra.exe' ) assert os.path.isfile( exe ) cmd = [exe, '--file', PREVIEW, '--config', TUNDRA_CONFIG_XML_PATH] p = subprocess.Popen(cmd, stdin=subprocess.PIPE) self.proc = p self.socket = sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) host='localhost'; port = 9978 sock.connect((host, port)) print('socket connected', sock) self._handle = None self.setup_callback( bpy.context ) self.ready = threading._allocate_lock() self.ID = threading._start_new_thread( self.loop, (None,) ) print( '.....thread started......')
def __init__(self): #Member Variables init. self.threadpool = [] self.FileNames = [] self.port_list = [] self.lo = TH._allocate_lock() self.bank = [] self.stats_buffer = [] self.is_started = False
def fix_main_thread_id(on_warn=lambda msg:None, on_exception=lambda msg:None, on_critical=lambda msg:None): # This means that we weren't able to import threading in the main thread (which most # likely means that the main thread is paused or in some very long operation). # In this case we'll import threading here and hotfix what may be wrong in the threading # module (if we're on Windows where we create a thread to do the attach and on Linux # we are not certain on which thread we're executing this code). # # The code below is a workaround for https://bugs.python.org/issue37416 import sys import threading try: with threading._active_limbo_lock: main_thread_instance = get_main_thread_instance(threading) if sys.platform == 'win32': # On windows this code would be called in a secondary thread, so, # the current thread is unlikely to be the main thread. if hasattr(threading, '_get_ident'): unlikely_thread_id = threading._get_ident() # py2 else: unlikely_thread_id = threading.get_ident() # py3 else: unlikely_thread_id = None main_thread_id, critical_warning = get_main_thread_id(unlikely_thread_id) if main_thread_id is not None: main_thread_id_attr = '_ident' if not hasattr(main_thread_instance, main_thread_id_attr): main_thread_id_attr = '_Thread__ident' assert hasattr(main_thread_instance, main_thread_id_attr) if main_thread_id != getattr(main_thread_instance, main_thread_id_attr): # Note that we also have to reset the '_tstack_lock' for a regular lock. # This is needed to avoid an error on shutdown because this lock is bound # to the thread state and will be released when the secondary thread # that initialized the lock is finished -- making an assert fail during # process shutdown. main_thread_instance._tstate_lock = threading._allocate_lock() main_thread_instance._tstate_lock.acquire() # Actually patch the thread ident as well as the threading._active dict # (we should have the _active_limbo_lock to do that). threading._active.pop(getattr(main_thread_instance, main_thread_id_attr), None) setattr(main_thread_instance, main_thread_id_attr, main_thread_id) threading._active[getattr(main_thread_instance, main_thread_id_attr)] = main_thread_instance # Note: only import from pydevd after the patching is done (we want to do the minimum # possible when doing that patching). on_warn('The threading module was not imported by user code in the main thread. The debugger will attempt to work around https://bugs.python.org/issue37416.') if critical_warning: on_critical('Issue found when debugger was trying to work around https://bugs.python.org/issue37416:\n%s' % (critical_warning,)) except: on_exception('Error patching main thread id.')
def __init__(self, master, slave): self.max_conn = 30 self.lock = threading._allocate_lock() self.curr = 0 self.conn_list = [] i = 0 while i < self.max_conn: self.conn_list.append(MysqlClient(master, slave)) i += 1
def wait(self, timeout=None): waiter = _allocate_lock() waiter.acquire() # get it the first time, no blocking self.append(waiter) try: # restore state no matter what (e.g., KeyboardInterrupt) # now we block, as we hold the lock already # in the momemnt we release our lock, someone else might actually resume self._lock.release() if timeout is None: waiter.acquire() else: # Balancing act: We can't afford a pure busy loop, because of the # GIL, so we have to sleep # We try to sleep only tiny amounts of time though to be very responsive # NOTE: this branch is not used by the async system anyway, but # will be hit when the user reads with timeout endtime = _time() + timeout delay = self.delay acquire = waiter.acquire while True: gotit = acquire(0) if gotit: break remaining = endtime - _time() if remaining <= 0: break # this makes 4 threads working as good as two, but of course # it causes more frequent micro-sleeping #delay = min(delay * 2, remaining, .05) _sleep(delay) # END endless loop if not gotit: try: self.remove(waiter) except AttributeError: # handle python 2.4 - actually this should be made thread-safe # but lets see ... try: # lets hope we pop the right one - we don't loop over it # yet-we just keep minimal compatability with py 2.4 item = self.pop() if item != waiter: self.append(item) except IndexError: pass except ValueError: pass # END didn't ever get it finally: # reacquire the lock self._lock.acquire()
def __init__(self, source=""): self.friends = {} self.typing = {} self.source = source self.lastMsgID = 0 self.rosterSet = None self.username = None self.resources = set([]) self.settings = Settings(source) self.last_udate = time.time() self.sync = threading._allocate_lock() logger.debug("User initialized (jid: %s)", self.source)
def __init__(self, width=640, height=480, active=True): self.active = active self.lock = threading._allocate_lock() self.index = 0 #self.ready = os.path.exists('/dev/video0') # Only on ubuntu, breaks fedora #self.cam = gui.CreateCameraCapture(self.index) self.cam = DEFAULT_WEBCAM_CAPTURE print(self.cam) self.ready = True ########### Layer Configs ############ self.layers = [] for colorspace in self._default_spaces: self.layers.append( LayerConfig( colorspace ) ) self.layers[0].active = True self.resize( width, height )
def __init__(self, width=640, height=480, active=True): self.active = active self.lock = threading._allocate_lock() self.index = 0 #self.ready = os.path.exists('/dev/video0') # Only on ubuntu, breaks fedora #self.cam = gui.CreateCameraCapture(self.index) self.cam = DEFAULT_WEBCAM_CAPTURE print(self.cam) self.ready = True ########### Layer Configs ############ self.layers = [] for colorspace in self._default_spaces: self.layers.append(LayerConfig(colorspace)) self.layers[0].active = True self.resize(width, height)
def __init__(self, port="/dev/ttyUSB0", baud=115200, timeout=0.1, open_port=True): self._mutex = threading._allocate_lock() self._ser = serial.Serial() self._ser.port = port self._ser.baudrate = baud self._ser.timeout = timeout if open_port: self._ser.open() ## The last error level read back self.error = 0
def __init__(self, root, regex, suff=('.py', '.java'), case=False, context=()): self.root = root self.regex = re.compile(regex) if case else re.compile(regex, re.IGNORECASE) self.suff = suff self.q = Queue() # shared thread-safe var for sub-results self.results = [] # used only for run with threads self.chunk_size = 100 self.n_threads = 3 self._sentinel = object() # signal end to daemon thread self.safeprint = threading._allocate_lock() self.stats = collections.Counter() self.live_res = True self.context = context if not regex: print '[Error] No search string given' sys.exit(0)
def wait(self, timeout=None): if not self._is_owned(): raise RuntimeError("cannot wait on un-acquired lock") waiter = _allocate_lock() waiter.acquire() self.__waiters.append(waiter) saved_state = self._release_save() try: # restore state no matter what (e.g., KeyboardInterrupt) if timeout is None: waiter.acquire() if __debug__: self._note("%s.wait(): got it", self) else: # Balancing act: We can't afford a pure busy loop, so we # have to sleep; but if we sleep the whole timeout time, # we'll be unresponsive. The scheme here sleeps very # little at first, longer as time goes on, but never longer # than 20 times per second (or the timeout time remaining). endtime = _time() + timeout delay = 0.0005 # 500 us -> initial delay of 1 ms while True: gotit = waiter.acquire(0) if gotit: break remaining = endtime - _time() if remaining <= 0: break delay = min(delay * 2, remaining, .05) _sleep(delay) if not gotit: if __debug__: self._note("%s.wait(%s): timed out", self, timeout) try: self.__waiters.remove(waiter) except ValueError: pass else: if __debug__: self._note("%s.wait(%s): got it", self, timeout) finally: self._acquire_restore(saved_state)
def __init__(self, db): """ db - HRDF-DB """ self.__hrdfdb = db self.__importFileName = "unknown" self.__eckdatenid = -1 self.__generateFrom = datetime.now().date() self.__generateTo = datetime.now().date() self.__TTGCache = HrdfTTGCache(db) self.__workQueue = Queue() self.__commQueue = Queue() self.__responseQueue = Queue() self.__responseData = dict() self.__numOfCurrentDBThreads = 0 self.__DBThreadStarted = False self.__lock = _allocate_lock() self.__numberOfWorker = 5 self.__chunkSize = 5000
def __init__(self, data=(), source=""): self.password = None self.username = None if data: self.username, self.password = data self.source = source self.auth = None self.token = None self.exists = None self.rosterSet = None self.lastMsgID = 0 self.typing = {} self.friends = {} self.chatUsers = {} self.hashes = {} self.resources = set([]) self.settings = Settings(source) self.last_udate = time.time() self.__sync = threading._allocate_lock() self.vk = VK(self.username, self.password, self.source) logger.debug("initializing User (jid: %s)" % self.source)
def main(): print('starting time at:', ctime()) locks = [] loopids = range(len(loops)) for i in loopids: # 创建锁对象 lock = threading._allocate_lock() # 锁定 lock.acquire() # 添加到locks数组 locks.append(lock) # 执行多线程 for i in loopids: threading._start_new_thread(loop, (i, loops[i], locks[i])) for i in loopids: while locks[i].locked(): pass print('all end at:', ctime())
def wait(self, timeout=None): if not self._is_owned(): raise RuntimeError("cannot wait on un-acquired lock") waiter = _allocate_lock() waiter.acquire() self._waiters.append(waiter) saved_state = self._release_save() try: # restore state no matter what (e.g., KeyboardInterrupt) if timeout is None: waiter.acquire() gotit = True else: if timeout > 0: gotit = waiter.acquire(True, timeout) else: gotit = waiter.acquire(False) if not gotit: try: self._waiters.remove(waiter) except ValueError: pass return gotit finally: self._acquire_restore(saved_state)
def start_threads(self): # Starts worker threads to collect data if not self.is_started: self.is_started = True self.lo.acquire( False ) # acquire lock telling workers not to break current task for j in range(0, len(self.port_list)): self.bank.append(TH._allocate_lock()) for i in range(0, len(self.port_list)): #print("Starting thread, file: " + NamesList[i] + " Port: " + str(AvailablePorts[i]) + "\n") self.stats_buffer.append(ring_buffer()) t = TH.Thread(target=DataCollection, args=(self.port_list[i], self.FileNames[i], i, self.lo, self.bank[i], self.stats_buffer[i])) self.threadpool.append(t) for t in self.threadpool: t.start() #start all threads t = TH.Thread(target=detection, args=(self.bank, self.lo)) self.threadpool.append(t) t.start() else: print("Already started!")
def __init__(self): #Member Variables init. self.threadpool = [] self.FileNames = [] self.lo = TH._allocate_lock() self.sem = TH.Semaphore(2)
import threading import curses import time import random command = None command_lock = threading._allocate_lock() stdscr = curses.initscr() stdscr.nodelay(1) def input_handler(): print('input_handler') # do not wait for input when calling getch global command, command_lock tmp_command = None while True: # stdscr.clear() # stdscr.move(0, 0) # stdscr.refresh() c = stdscr.getch() if c == -1: continue c = chr(c) if c == 'w': tmp_command = 'forward' elif c == 'a': tmp_command = 'turn_left'
from threading import _start_new_thread, _allocate_lock from time import sleep # globale Variable num_threads = 0 thread_started = False lock = _allocate_lock() def heron(a): global num_threads, thread_started lock.acquire() # bleibt auf jeden Fall stehen bei Threadwechsel num_threads += 1 thread_started = True lock.release() sleep(1) print(a) num_threads -= 1 return 42 # -------------------------------------------------------- _start_new_thread(heron, (99, )) _start_new_thread(heron, (999, )) _start_new_thread(heron, (1733, )) _start_new_thread(heron, (17334, ))
def __init__(self, safe=True): self._safe=safe self._lock = threading.Lock() self._acquire_stack=deque() self._waiter=_allocate_lock() self._waiter.acquire()
nclntlock.release() # set up internet TCP socket lstn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) port = int(sys.argv[1]) #server port number # bind lstn socket to this port lstn.bind(('', port)) # start listening for contacts from clients (at most 2 at a time) lstn.listen(5) #initialize v, total v = '' # set up a lock to guard v vlock = threading._allocate_lock() #nclnt will be the number of clients still connected nclnt = 2 # set up a lock to guard nclnt nclntlock = threading._allocate_lock() #accept calls from the clients for i in range(nclnt): # wait for call, then get a new socket to use for this client and get the client's address/port tuple (though not used) (clnt, ap) = lstn.accept() # start thread for this client, with serveclient() as the thread’s # function, with parameters clnt; note that parameter set must be # a tuple; in this case, the tuple is of length 1, so a comma is # needed
con.surface.soft_erp += ob2._soft_erp con.surface.soft_cfm += ob2._soft_cfm else: con.surface.mode = ode.ContactBounce con.surface.bounce = ob1._bounce + ob2._bounce info = {'location':g.pos, 'normal':g.normal, 'depth':g.depth} ob1._touching[ ob2.name ] = info ob2._touching[ ob1.name ] = info joint = ode.JointCreateContact( self.world, self.joint_group, ctypes.pointer(con) ) joint.Attach( body1, body2 ) LOCK = threading._allocate_lock() if hasattr(ode, 'InitODE'): ENGINE = OdeSingleton( lock=LOCK ) else: ENGINE = None print('warning can not load ODE library') ############################################################ class Joint( object ): Types = { 'ball' : 'Ball', 'hinge' : 'Hinge', 'slider' : 'Slider', 'universal' : 'Universal', 'dual-hinge' : 'Hinge2', 'fixed' : 'Fixed',
@author: huaihuai ''' import threading import time from xmlrpc.server import SimpleXMLRPCServer, socketserver # RPC-多线程 class RPCThreading(socketserver.ThreadingMixIn, SimpleXMLRPCServer): pass # 全局锁 global mutex mutex = threading._allocate_lock() class RPCMultithreading(object): def __init__(self, ip, port): self.ip = ip self.port = port def create(self): server_object = Server() server = RPCThreading((self.ip, self.port), logRequests=False) server.allow_none = True server.register_instance(server_object) print('多线程RPC服务器已开启......') server.serve_forever()
def __init__(self): self.__lock = _allocate_lock() super(Lock,self).__init__()
def __init__(self, iface): self.iface = iface self.startStopLock = threading._allocate_lock() self.getter = None self.newDataList = [] self.oldDataDict = dict()
# The inner attribute changed between 2 and 3 attr = getattr(lock, '_block' if not PY2 else '_RLock__block', None) return attr def checkLocks(kind, ignore_none=True): handlers = logging._handlerList assert len(handlers) > 0 for weakref in handlers: # In py26, these are actual handlers, not weakrefs handler = weakref() if callable(weakref) else weakref attr = _inner_lock(handler.lock) if attr is None and ignore_none: continue assert isinstance(attr, kind), (handler.lock, attr, kind) attr = _inner_lock(logging._lock) if attr is None and ignore_none: return assert isinstance(attr, kind) checkLocks(type(threading._allocate_lock())) import gevent.monkey gevent.monkey.patch_all() import gevent.lock checkLocks(type(gevent.thread.allocate_lock()), ignore_none=False)
import persistent from Acquisition import Implicit from Acquisition import ImplicitAcquisitionWrapper from Acquisition import aq_base from ZODB.POSException import StorageError logger = logging.getLogger('ZODB.Mount') # dbs is a holder for all DB objects, needed to overcome # threading issues. It maps connection params to a DB object # and a mapping of mount points. dbs = {} # dblock is locked every time dbs is accessed. dblock = threading._allocate_lock() class MountedStorageError(StorageError): """Unable to access mounted storage.""" def parentClassFactory(jar, module, name): # Use the class factory from the parent database. parent_conn = getattr(jar, '_mount_parent_jar', None) parent_db = getattr(parent_conn, '_db', None) if parent_db is None: _globals = {} _silly = ('__doc__',) return getattr(__import__( module, _globals, _globals, _silly), name)
#! /usr/bin/env python # -*- coding:utf-8 -*- numconsumers = 1 numproducers = 4 nummessages = 4 import threading, queue, time dataQueque = queue.Queue() safeprint = threading._allocate_lock() def producer(idnum, dataqueque): for msgnum in range(nummessages): time.sleep(idnum) dataqueque.put('[producer id=%d, count=%d]' % (idnum, msgnum)) def consumer(idnum, dataqueque): while True: time.sleep(0.1) try: data = dataqueque.get(block=False) except queue.Empty: pass else: with safeprint: print('consumer', idnum, 'got ==>', data) if __name__ == '__main__':
#!/usr/bin/python # -*- coding: utf-8 -*- #import _thread #import time #import random #import sys #import threading from threading import Thread, _allocate_lock, Event import time import random from queue import Queue from collections import deque queue = Queue(10) lock = _allocate_lock() #objeto para bloquear/liberar as variáveis event = Event() #length_max = int(sys.argv[1])# #s = _thread.allocate_lock() # buffer de itens, 1 para cada allowed_type, maximo 8 posicoes buffer_itens = {1: [], 2: [], 3: [], 4: [], 5: []} global buffer_list buffer_list = [] num_producer = 5 #int(input('Digite numero de produceres: ')) num_producer = (num_producer if num_producer <= 5 else 5) num_consumer = 5 #int(input('Digite numero de consumeres: ')) num_consumer = (num_consumer if num_consumer <= 5 else 5) print(num_producer, num_consumer)
def __init__(self, howMany): self._lock = threading._allocate_lock() self._results = {} self._finished = 0 self._total = howMany
def __init__(self): self._lock = threading._allocate_lock() super(Lock, self).__init__()
import threading import time import random global buffer lock = threading._allocate_lock() #objeto para bloquear/liberar as variáveis event = threading.Event() class Produtor(threading.Thread): def __init__(self, tipos, id): super(Produtor, self).__init__() self.tipos = list(tipos) self.id = id self.count_item = 0 self.repeticoes = 3 '''Run() é onde as ações da thread são executadas.''' def run(self): while self.tipos: #print("Iniciando produtor ",self.id) tipo = self.tipos.pop(self.tipos.index(random.choice( self.tipos))) #selecionando um tipo aleatóriamente valor = random.randint(0, 3000) lock.acquire() print("Produtor {}, produzindo item do tipo {} ({}ms).".format( self.id, tipo, valor)) self.count_item += 1 item = Item(tipo, self.id, self.count_item, valor) buffer.append(item) tempo_espera = random.randint(0, 3000)
inner_semaphore, root ) ) def checkLocks(kind, ignore_none=True): handlers = logging._handlerList assert handlers for weakref in handlers: # In py26, these are actual handlers, not weakrefs handler = weakref() if callable(weakref) else weakref block = _inner_lock(handler.lock) if block is None and ignore_none: continue _check_type(handler, handler.lock, block, kind) attr = _inner_lock(logging._lock) if attr is None and ignore_none: return _check_type(logging, logging._lock, attr, kind) checkLocks(type(threading._allocate_lock())) import gevent.monkey gevent.monkey.patch_all() import gevent.lock checkLocks(type(gevent.thread.allocate_lock()), ignore_none=False)
#!/usr/bin/env python3 # encoding:utf-8 # written by:liuzhaoyang import time, threading def counter(my_id, count): for i in range(count): time.sleep(2) mutex.acquire() print('[%s]=>>>>%s===>>%s' % (my_id, i, time.time())) mutex.release() mutex = threading._allocate_lock() for i in range(5): threading._start_new_thread(counter, (i, 5)) time.sleep(6) print('main thread exiting')
def parallel(mods, modargs=[]): """ Run a set of tests in parallel, rather than in sequence, then collect and print their results. Mods is a set of python modules, and then each of their corresponding files is run with arguments from modargs, assuming the files are executable with python mods: python modules containing tests. Each module must be runnable with python mod.__file__, which usually means each file needs a runnable __main__ which runs some tests modargs: set of args which can be passed to each mod if it is a list, each entry of the list will be iterated over with the mod (sub-lists will be hjoined with " ".join() ) if it is a dictionary of mod : [list] each entry in the list will be iterated over for the corresponding mod (sub-lists will be joined with " ".join() ) e.g. mods=[foo,bar,fish], modargs={'foo':["spam","eggs"], 'bar':[["spam","and","eggs"]]} will run four tests in parallel: python foo.__file__ spam python foo.__file__ eggs python bar.__file__ spam and eggs python fish.__file__ """ # loop over threads, see the class for more details # create list of packages as a queue item_pool = queue.Queue() result = {} items = [] if not len(modargs): items = ["python " + mod.__file__.replace('.pyc', ".py") for mod in mods] elif type(modargs) is list: for mod in mods: for arg in modargs: if type(arg) is list: arg = " ".join(arg) items.append("python " + mod.__file__.replace('.pyc', ".py") + " " + arg) elif type(modargs) is dict: for mod in mods: if mod not in modargs: items.append("python " + mod.__file__.replace('.pyc', ".py")) continue args = modargs[mod] # just a single arguement if type(args) is not list: items.append("python " + mod.__file__.replace('.pyc', ".py") + " " + args) continue # empty list if not len(args): items.append("python " + mod.__file__.replace('.pyc', ".py")) continue for arg in args: if type(arg) is list: arg = " ".join(arg) items.append("python " + mod.__file__.replace('.pyc', ".py") + " " + arg) else: raise ValueError("failed to interpret mod and modargs") # print items for item in items: result[item] = {} item_pool.put(item) lock = threading._allocate_lock() thethreads = [] for _i in range(20): t = RunFileInSubProcess(item_pool, lock, result) thethreads.append(t) t.start() # setup a timeout to prevent really infinite loops! import datetime import time begin = datetime.datetime.utcnow() timeout = 60 * 60 * 3 for t in thethreads: while not t.done: if (datetime.datetime.utcnow() - begin).seconds > timeout: break time.sleep(0.1) nd = [t for t in thethreads if not t.done] errs = [] for t in thethreads: errs = errs + t.errors if len(errs): raise RuntimeError("Exceptions encountered while running tests as threads, as: \n" + '\n'.join(errs)) # print result FAILED = len([f for f in result if result[f]["status"] != 0]) TIMES = [] TESTS = [] for key, val in result.items(): timing = [l for l in val["stderr"].split(b"\n") if l.startswith(b"Ran") and b" in " in l][0].strip() if len(timing): TIMES.append(float(timing.split(b" ")[-1].replace(b"s", b""))) TESTS.append(int(timing.split(b" ")[1])) print("======================================================================") print("Ran", sum(TESTS), "tests in", sum(TIMES).__str__() + "s", "from", len(result), "module/args") print('') if FAILED == 0 and len(nd) == 0: print("OK") sys.exit(0) if FAILED: print("FAILED (modules=" + str(FAILED) + ")") if len(nd): print("TIMEOUT (modules=" + str(len(nd)) + ")") sys.exit(1)
import threading import queue import spider import htmlparser import tools import time import comment qplace = queue.Queue() #place of url 队列。 qsight = queue.Queue() #sight of url 队列 setplace = set() #存放place of url的md5值的bytes,防止重复采集url setsight = set() #存放sight of url mutexOfplace = threading._allocate_lock()#分配一个线程锁 mutexOfsight = threading._allocate_lock()#分配一个线程锁 resource = 'http://you.ctrip.com/destinationsite/TTDSecond/SharedView/AsynCommentView' def worksight(): if qsight.empty() == True: time.sleep(5) while qsight.empty() != True: url = tools.work(qsight,setsight,mutexOfsight) if url : if url.startswith("http://you.ctrip.com"): pass else: url = 'http://you.ctrip.com' + url html_code = spider.request_html(url) time.sleep(1)
# line_match = [] # for f in found: # lines = search_file(f) # if lines: # line_match.append((f, lines)) # collect_live_res(line_match) from threading import Thread from Queue import Queue, Empty q = Queue() f_size = 100 search_res = [] safeprint = threading._allocate_lock() # signal to daemon thread to stop execution, unecessary _sentinel = object() nthreads = 3 file_chunks = [found[i:i+f_size] for i in xrange(0, len(found)-1, f_size)] def tr_print(msg, mute=True): """Print on thread - no text overlap""" if mute: return with safeprint: print msg def producer(files, q): line_match = []
note.set_tab_pos(gtk.POS_RIGHT) root.pack_start(note, expand=True) for layer in self.webcam.layers: layer.widget(note) def on_click(self, image): self.active = not self.active if self.active and not self.webcam.active: self.webcam.active = self.active self.webcam.start_thread() else: self.webcam.active = self.active if __name__ == '__main__': win = gtk.Window() win.set_title('OpenCV+GTK') widget = Widget(win) win.connect('destroy', widget.exit) win.show_all() lock = threading._allocate_lock() widget.webcam.start_thread(lock) while widget.active: #widget.webcam.iterate() # threading is way better lock.acquire() while gtk.gtk_events_pending(): gtk.gtk_main_iteration() lock.release()
def parallel(mods, modargs=[]): """ Run a set of tests in parallel, rather than in sequence, then collect and print their results. Mods is a set of python modules, and then each of their corresponding files is run with arguments from modargs, assuming the files are executable with python mods: python modules containing tests. Each module must be runnable with python mod.__file__, which usually means each file needs a runnable __main__ which runs some tests modargs: set of args which can be passed to each mod if it is a list, each entry of the list will be iterated over with the mod (sub-lists will be hjoined with " ".join() ) if it is a dictionary of mod : [list] each entry in the list will be iterated over for the corresponding mod (sub-lists will be joined with " ".join() ) e.g. mods=[foo,bar,fish], modargs={'foo':["spam","eggs"], 'bar':[["spam","and","eggs"]]} will run four tests in parallel: python foo.__file__ spam python foo.__file__ eggs python bar.__file__ spam and eggs python fish.__file__ """ # loop over threads, see the class for more details # create list of packages as a queue item_pool = queue.Queue() result = {} items = [] if not len(modargs): items = [ "python " + mod.__file__.replace('.pyc', ".py") for mod in mods ] elif type(modargs) is list: for mod in mods: for arg in modargs: if type(arg) is list: arg = " ".join(arg) items.append("python " + mod.__file__.replace('.pyc', ".py") + " " + arg) elif type(modargs) is dict: for mod in mods: if mod not in modargs: items.append("python " + mod.__file__.replace('.pyc', ".py")) continue args = modargs[mod] # just a single arguement if type(args) is not list: items.append("python " + mod.__file__.replace('.pyc', ".py") + " " + args) continue # empty list if not len(args): items.append("python " + mod.__file__.replace('.pyc', ".py")) continue for arg in args: if type(arg) is list: arg = " ".join(arg) items.append("python " + mod.__file__.replace('.pyc', ".py") + " " + arg) else: raise ValueError("failed to interpret mod and modargs") # print items for item in items: result[item] = {} item_pool.put(item) lock = threading._allocate_lock() thethreads = [] for _i in range(20): t = RunFileInSubProcess(item_pool, lock, result) thethreads.append(t) t.start() # setup a timeout to prevent really infinite loops! import datetime import time begin = datetime.datetime.utcnow() timeout = 60 * 60 * 3 for t in thethreads: while not t.done: if (datetime.datetime.utcnow() - begin).seconds > timeout: break time.sleep(0.1) nd = [t for t in thethreads if not t.done] errs = [] for t in thethreads: errs = errs + t.errors if len(errs): raise RuntimeError( "Exceptions encountered while running tests as threads, as: \n" + '\n'.join(errs)) # print result FAILED = len([f for f in result if result[f]["status"] != 0]) TIMES = [] TESTS = [] for key, val in result.items(): timing = [ l for l in val["stderr"].split(b"\n") if l.startswith(b"Ran") and b" in " in l ][0].strip() if len(timing): TIMES.append(float(timing.split(b" ")[-1].replace(b"s", b""))) TESTS.append(int(timing.split(b" ")[1])) print( "======================================================================" ) print("Ran", sum(TESTS), "tests in", sum(TIMES).__str__() + "s", "from", len(result), "module/args") print('') if FAILED == 0 and len(nd) == 0: print("OK") sys.exit(0) if FAILED: print("FAILED (modules=" + str(FAILED) + ")") if len(nd): print("TIMEOUT (modules=" + str(len(nd)) + ")") sys.exit(1)
#!/usr/bin/env python3 # encoding:utf-8 # written by:liuzhaoyang import threading stdoutlock = threading._allocate_lock() exit_lock = [threading._allocate_lock() for i in range(10)] i = 0 def counter(my_id, count): for i in range(count): stdoutlock.acquire() print('[%s]=>>>>%s' % (my_id, i)) i += 1 stdoutlock.release() exit_lock[my_id].acquire() for i in range(10): threading._start_new_thread(counter, (i, 100)) for mutex in exit_lock: while not mutex.locked(): pass print('main threading exiting', i)