def __init__(self, keystr, bits=None): self.keystr = keystr self._key = None self.genlock = _thread.allocate() if keystr == None: self.generate(bits=int(bits) or 1024) else: self.set_key(rsalib.importKey(keystr.export()))
def test_start_new_thread(): #--Sanity global CALLED CALLED = False lock = _thread.allocate() def tempFunc(mykw_param=1): global CALLED lock.acquire() CALLED = mykw_param lock.release() _thread.exit_thread() id = _thread.start_new_thread(tempFunc, (), {"mykw_param": 7}) while CALLED == False: print(".", end=' ') time.sleep(0.1) AreEqual(CALLED, 7) id = _thread.start_new_thread(tempFunc, (), {"mykw_param": 8}) while CALLED != 8: #Hang forever if this is broken print(".", end=' ') time.sleep(0.1) #--Sanity Negative global temp_stderr temp_stderr = "" se = sys.stderr class myStdOut: def write(self, text): global temp_stderr temp_stderr += text try: sys.stderr = myStdOut() id = _thread.start_new_thread(tempFunc, (), {"my_misspelled_kw_param": 9}) time.sleep(1) if not is_silverlight: se.flush() finally: sys.stderr = se AreEqual(CALLED, 8) Assert( "tempFunc() got an unexpected keyword argument 'my_misspelled_kw_param" in temp_stderr)
def __init__(self, frame_source_types): # recipe to get address of surface: http://archives.seul.org/pygame/users/Apr-2008/msg00218.html is_64bits = sys.maxsize > 2**32 if not is_64bits: self.Py_ssize_t = ctypes.c_int else: self.Py_ssize_t = ctypes.c_int64 self._PyObject_AsWriteBuffer = ctypes.pythonapi.PyObject_AsWriteBuffer self._PyObject_AsWriteBuffer.restype = ctypes.c_int self._PyObject_AsWriteBuffer.argtypes = [ ctypes.py_object, ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(self.Py_ssize_t) ] #self._color_frame_ready = PyKinectV2._event() #self._depth_frame_ready = PyKinectV2._event() #self._body_frame_ready = PyKinectV2._event() #self._body_index_frame_ready = PyKinectV2._event() #self._infrared_frame_ready = PyKinectV2._event() #self._long_exposure_infrared_frame_ready = PyKinectV2._event() #self._audio_frame_ready = PyKinectV2._event() self._close_event = ctypes.windll.kernel32.CreateEventW( None, False, False, None) self._color_frame_arrived_event = 0 self._depth_frame_arrived_event = 0 self._body_frame_arrived_event = 0 self._body_index_frame_arrived_event = 0 self._infrared_frame_arrived_event = 0 self._long_exposure_infrared_frame_arrived_event = 0 self._audio_frame_arrived_event = 0 self._color_frame_lock = thread.allocate() self._depth_frame_lock = thread.allocate() self._body_frame_lock = thread.allocate() self._body_index_frame_lock = thread.allocate() self._infrared_frame_lock = thread.allocate() self._long_exposure_infrared_frame_lock = thread.allocate() self._audio_frame_lock = thread.allocate() #initialize sensor self._sensor = ctypes.POINTER(PyKinectV2.IKinectSensor)() hres = ctypes.windll.kinect20.GetDefaultKinectSensor( ctypes.byref(self._sensor)) hres = self._sensor.Open() self._mapper = self._sensor.CoordinateMapper self.frame_source_types = frame_source_types self.max_body_count = KINECT_MAX_BODY_COUNT self._handles = (ctypes.c_voidp * 8)() self._handles[0] = self._close_event self._handles[1] = self._close_event self._handles[2] = self._close_event self._handles[3] = self._close_event self._handles[4] = self._close_event self._handles[5] = self._close_event self._handles[6] = self._close_event self._handles[7] = self._close_event self._waitHandleCount = 1 self._color_source = self._sensor.ColorFrameSource self.color_frame_desc = self._color_source.FrameDescription self._depth_source = self._sensor.DepthFrameSource self.depth_frame_desc = self._depth_source.FrameDescription self._body_index_source = self._sensor.BodyIndexFrameSource self.body_index_frame_desc = self._body_index_source.FrameDescription self._body_source = self._sensor.BodyFrameSource self._body_frame_data = ctypes.POINTER(ctypes.POINTER(IBody)) self.max_body_count = self._body_source.BodyCount self._color_frame_data = None self._depth_frame_data = None self._body_frame_data = None self._body_index_frame_data = None self._infrared_frame_data = None self._long_exposure_infrared_frame_data = None self._audio_frame_data = None if (self.frame_source_types & FrameSourceTypes_Color): self._color_frame_data = ctypes.POINTER(ctypes.c_ubyte) self._color_frame_data_capacity = ctypes.c_uint( self.color_frame_desc.Width * self.color_frame_desc.Height * 4) self._color_frame_data_type = ctypes.c_ubyte * self._color_frame_data_capacity.value self._color_frame_data = ctypes.cast( self._color_frame_data_type(), ctypes.POINTER(ctypes.c_ubyte)) self._color_frame_reader = self._color_source.OpenReader() self._color_frame_arrived_event = self._color_frame_reader.SubscribeFrameArrived( ) self._handles[ self._waitHandleCount] = self._color_frame_arrived_event self._waitHandleCount += 1 if (self.frame_source_types & FrameSourceTypes_Depth): self._depth_frame_data = ctypes.POINTER(ctypes.c_ushort) self._depth_frame_data_capacity = ctypes.c_uint( self.depth_frame_desc.Width * self.depth_frame_desc.Height) self._depth_frame_data_type = ctypes.c_ushort * self._depth_frame_data_capacity.value self._depth_frame_data = ctypes.cast( self._depth_frame_data_type(), ctypes.POINTER(ctypes.c_ushort)) self._depth_frame_reader = self._depth_source.OpenReader() self._depth_frame_arrived_event = self._depth_frame_reader.SubscribeFrameArrived( ) self._handles[ self._waitHandleCount] = self._depth_frame_arrived_event self._waitHandleCount += 1 if (self.frame_source_types & FrameSourceTypes_BodyIndex): self._body_index_frame_data = ctypes.POINTER(ctypes.c_ubyte) self._body_index_frame_data_capacity = ctypes.c_uint( self.body_index_frame_desc.Width * self.body_index_frame_desc.Height) self._body_index_frame_data_type = ctypes.c_ubyte * self._body_index_frame_data_capacity.value self._body_index_frame_data = ctypes.cast( self._body_index_frame_data_type(), ctypes.POINTER(ctypes.c_ubyte)) self._body_index_frame_reader = self._body_index_source.OpenReader( ) self._body_index_frame_arrived_event = self._body_index_frame_reader.SubscribeFrameArrived( ) self._handles[ self._waitHandleCount] = self._body_index_frame_arrived_event self._waitHandleCount += 1 self._body_frame_data = None if (self.frame_source_types & FrameSourceTypes_Body): self._body_frame_data_capacity = ctypes.c_uint(self.max_body_count) self._body_frame_data_type = ctypes.POINTER( IBody) * self._body_frame_data_capacity.value self._body_frame_data = ctypes.cast( self._body_frame_data_type(), ctypes.POINTER(ctypes.POINTER(IBody))) self._body_frame_reader = self._body_source.OpenReader() self._body_frame_arrived_event = self._body_frame_reader.SubscribeFrameArrived( ) self._body_frame_bodies = None self._handles[ self._waitHandleCount] = self._body_frame_arrived_event self._waitHandleCount += 1 thread.start_new_thread(self.kinect_frame_thread, ()) self._last_color_frame = None self._last_depth_frame = None self._last_body_frame = None self._last_body_index_frame = None self._last_infrared_frame = None self._last_long_exposure_infrared_frame = None self._last_audio_frame = None start_clock = time.clock() self._last_color_frame_access = self._last_color_frame_time = start_clock self._last_body_frame_access = self._last_body_frame_time = start_clock self._last_body_index_frame_access = self._last_body_index_frame_time = start_clock self._last_depth_frame_access = self._last_depth_frame_time = start_clock self._last_infrared_frame_access = self._last_infrared_frame_time = start_clock self._last_long_exposure_infrared_frame_access = self._last_long_exposure_infrared_frame_time = start_clock self._last_audio_frame_access = self._last_audio_frame_time = start_clock
sys.path.append(sys.path[0] + '/vuldb') sys.path.append(sys.path[0] + "/../") from config import ProductionConfig db_conn = pymongo.MongoClient(ProductionConfig.DB, ProductionConfig.PORT) na_db = getattr(db_conn, ProductionConfig.DBNAME) na_db.authenticate(ProductionConfig.DBUSERNAME, ProductionConfig.DBPASSWORD) na_task = na_db.Task na_result = na_db.Result na_plugin = na_db.Plugin na_config = na_db.Config na_heart = na_db.Heartbeat na_update = na_db.Update lock = thread.allocate() PASSWORD_DIC = [] THREAD_COUNT = 50 TIMEOUT = 10 PLUGIN_DB = {} TASK_DATE_DIC = {} WHITE_LIST = [] kp = kunpeng() class vulscan(): def __init__(self, task_id, task_netloc, task_plugin): self.task_id = task_id self.task_netloc = task_netloc self.task_plugin = task_plugin self.result_info = ''
def __init__(self): self.mutex = thread.allocate() self.todo = thread.allocate() self.todo.acquire() self.work = [] self.busy = 0
print(arg, i) time.sleep(1) print(arg, 'thread param') # 结束当前线程 # 这个方法与_thread.exit_thread()等价 _thread.exit() # 当func返回时,线程同样会结束 # 启动一个线程,线程立即开始运行 # 这个方法与_thread.start_new_thread()等价 # 第一个参数是方法,第二个参数是方法的参数 _thread.start_new(foo, ("线程1", )) # 方法没有参数时需要传入空tuple # 创建一个锁(LockType,不能直接实例化) # 这个方法与_thread.allocate_lock()等价 lock = _thread.allocate() # 判断锁是锁定状态还是释放状态 print(lock.locked()) # 输出为False # 锁通常用于控制对共享资源的访问 count = 0 # 获得锁,成功获得锁定后返回True # 可选的timeout参数不填时将一直阻塞直到获得锁定 # 否则超时后将返回False if lock.acquire(): print(lock.locked()) # 输出为True # 释放锁 lock.release() # _thread模块提供的线程都将在主线程结束后同时结束, time.sleep(4)
def __init__(self, frame_source_types): # recipe to get address of surface: http://archives.seul.org/pygame/users/Apr-2008/msg00218.html is_64bits = sys.maxsize > 2**32 if not is_64bits: self.Py_ssize_t = ctypes.c_int else: self.Py_ssize_t = ctypes.c_int64 self._PyObject_AsWriteBuffer = ctypes.pythonapi.PyObject_AsWriteBuffer self._PyObject_AsWriteBuffer.restype = ctypes.c_int self._PyObject_AsWriteBuffer.argtypes = [ctypes.py_object, ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(self.Py_ssize_t)] #self._color_frame_ready = PyKinectV2._event() #self._depth_frame_ready = PyKinectV2._event() #self._body_frame_ready = PyKinectV2._event() #self._body_index_frame_ready = PyKinectV2._event() #self._infrared_frame_ready = PyKinectV2._event() #self._long_exposure_infrared_frame_ready = PyKinectV2._event() #self._audio_frame_ready = PyKinectV2._event() self._close_event = ctypes.windll.kernel32.CreateEventW(None, False, False, None) self._color_frame_arrived_event = 0 self._depth_frame_arrived_event = 0 self._body_frame_arrived_event = 0 self._body_index_frame_arrived_event = 0 self._infrared_frame_arrived_event = 0 self._long_exposure_infrared_frame_arrived_event = 0 self._audio_frame_arrived_event = 0 self._color_frame_lock = thread.allocate() self._depth_frame_lock = thread.allocate() self._body_frame_lock = thread.allocate() self._body_index_frame_lock = thread.allocate() self._infrared_frame_lock = thread.allocate() self._long_exposure_infrared_frame_lock = thread.allocate() self._audio_frame_lock = thread.allocate() #initialize sensor self._sensor = ctypes.POINTER(PyKinectV2.IKinectSensor)() hres = ctypes.windll.kinect20.GetDefaultKinectSensor(ctypes.byref(self._sensor)) hres = self._sensor.Open() self._mapper = self._sensor.CoordinateMapper self.frame_source_types = frame_source_types self.max_body_count = KINECT_MAX_BODY_COUNT self._handles = (ctypes.c_voidp * 8)() self._handles[0] = self._close_event self._handles[1] = self._close_event self._handles[2] = self._close_event self._handles[3] = self._close_event self._handles[4] = self._close_event self._handles[5] = self._close_event self._handles[6] = self._close_event self._handles[7] = self._close_event self._waitHandleCount = 1 self._color_source = self._sensor.ColorFrameSource self.color_frame_desc = self._color_source.FrameDescription self._infrared_source = self._sensor.InfraredFrameSource self.infrared_frame_desc = self._infrared_source.FrameDescription self._depth_source = self._sensor.DepthFrameSource self.depth_frame_desc = self._depth_source.FrameDescription self._body_index_source = self._sensor.BodyIndexFrameSource self.body_index_frame_desc = self._body_index_source.FrameDescription self._body_source = self._sensor.BodyFrameSource self._body_frame_data = ctypes.POINTER(ctypes.POINTER(IBody)) self.max_body_count = self._body_source.BodyCount self._color_frame_data = None self._depth_frame_data = None self._body_frame_data = None self._body_index_frame_data = None self._infrared_frame_data = None self._long_exposure_infrared_frame_data = None self._audio_frame_data = None if(self.frame_source_types & FrameSourceTypes_Color): self._color_frame_data = ctypes.POINTER(ctypes.c_ubyte) self._color_frame_data_capacity = ctypes.c_uint(self.color_frame_desc.Width * self.color_frame_desc.Height * 4) self._color_frame_data_type = ctypes.c_ubyte * self._color_frame_data_capacity.value self._color_frame_data = ctypes.cast(self._color_frame_data_type(), ctypes.POINTER(ctypes.c_ubyte)) self._color_frame_reader = self._color_source.OpenReader() self._color_frame_arrived_event = self._color_frame_reader.SubscribeFrameArrived() self._handles[self._waitHandleCount] = self._color_frame_arrived_event self._waitHandleCount += 1 if(self.frame_source_types & FrameSourceTypes_Infrared): self._infrared_frame_data = ctypes.POINTER(ctypes.c_ushort) self._infrared_frame_data_capacity = ctypes.c_uint(self.infrared_frame_desc.Width * self.infrared_frame_desc.Height) self._infrared_frame_data_type = ctypes.c_ushort * self._infrared_frame_data_capacity.value self._infrared_frame_data = ctypes.cast(self._infrared_frame_data_type(), ctypes.POINTER(ctypes.c_ushort)) self._infrared_frame_reader = self._infrared_source.OpenReader() self._infrared_frame_arrived_event = self._infrared_frame_reader.SubscribeFrameArrived() self._handles[self._waitHandleCount] = self._infrared_frame_arrived_event self._waitHandleCount += 1 if(self.frame_source_types & FrameSourceTypes_Depth): self._depth_frame_data = ctypes.POINTER(ctypes.c_ushort) self._depth_frame_data_capacity = ctypes.c_uint(self.depth_frame_desc.Width * self.depth_frame_desc.Height) self._depth_frame_data_type = ctypes.c_ushort * self._depth_frame_data_capacity.value self._depth_frame_data = ctypes.cast(self._depth_frame_data_type(), ctypes.POINTER(ctypes.c_ushort)) self._depth_frame_reader = self._depth_source.OpenReader() self._depth_frame_arrived_event = self._depth_frame_reader.SubscribeFrameArrived() self._handles[self._waitHandleCount] = self._depth_frame_arrived_event self._waitHandleCount += 1 if(self.frame_source_types & FrameSourceTypes_BodyIndex): self._body_index_frame_data = ctypes.POINTER(ctypes.c_ubyte) self._body_index_frame_data_capacity = ctypes.c_uint(self.body_index_frame_desc.Width * self.body_index_frame_desc.Height) self._body_index_frame_data_type = ctypes.c_ubyte * self._body_index_frame_data_capacity.value self._body_index_frame_data = ctypes.cast(self._body_index_frame_data_type(), ctypes.POINTER(ctypes.c_ubyte)) self._body_index_frame_reader = self._body_index_source.OpenReader() self._body_index_frame_arrived_event = self._body_index_frame_reader.SubscribeFrameArrived() self._handles[self._waitHandleCount] = self._body_index_frame_arrived_event self._waitHandleCount += 1 self._body_frame_data = None if(self.frame_source_types & FrameSourceTypes_Body): self._body_frame_data_capacity = ctypes.c_uint(self.max_body_count) self._body_frame_data_type = ctypes.POINTER(IBody) * self._body_frame_data_capacity.value self._body_frame_data = ctypes.cast(self._body_frame_data_type(), ctypes.POINTER(ctypes.POINTER(IBody))) self._body_frame_reader = self._body_source.OpenReader() self._body_frame_arrived_event = self._body_frame_reader.SubscribeFrameArrived() self._body_frame_bodies = None self._handles[self._waitHandleCount] = self._body_frame_arrived_event self._waitHandleCount += 1 thread.start_new_thread(self.kinect_frame_thread, ()) self._last_color_frame = None self._last_depth_frame = None self._last_body_frame = None self._last_body_index_frame = None self._last_infrared_frame = None self._last_long_exposure_infrared_frame = None self._last_audio_frame = None start_clock = time.clock() self._last_color_frame_access = self._last_color_frame_time = start_clock self._last_body_frame_access = self._last_body_frame_time = start_clock self._last_body_index_frame_access = self._last_body_index_frame_time = start_clock self._last_depth_frame_access = self._last_depth_frame_time = start_clock self._last_infrared_frame_access = self._last_infrared_frame_time = start_clock self._last_long_exposure_infrared_frame_access = self._last_long_exposure_infrared_frame_time = start_clock self._last_audio_frame_access = self._last_audio_frame_time = start_clock
""" An example that shows how to draw the depth map of a scene """ import _thread import pygame from pykinect import nui DEPTH_WINSIZE = 320, 240 screen_lock = _thread.allocate() screen = None tmp_s = pygame.Surface(DEPTH_WINSIZE, 0, 16) def depth_frame_ready(frame): with screen_lock: # Copy raw data in a temp surface frame.image.copy_bits(tmp_s._pixels_address) # Get actual depth data in mm arr2d = (pygame.surfarray.pixels2d(tmp_s) >> 3) & 4095 # Process depth data as you prefer # arr2d = some_function(arr2d) # Get an 8-bit depth map (useful to be drawn as a grayscale image) arr2d >>= 4 # Copy the depth map in the main surface
#!/usr/bin/python3 import sys, time, json, _thread import http.client, urllib.parse import requests host = 'http://dlshop.weitshop.cn/?r=h5&shopid=7&from=h5#/shop/appGoods/list' thread_count = 100 #并发数量 now_count = 0 error_count = 0 begin_time = '' lock_obj = _thread.allocate() def test_http_engine(): global now_count global error_count global thread_count global begin_time conn = None if now_count == 0: begin_time = int(round(time.time() * 1000)) try: response = requests.get(url=host) data = response.read() print(data) if json.dumps(response.status) != '200': error_count += 1
"""объект мьютекса, совместно используемый всеми потоками выполнения, передается функции в виде аргумента; для автоматического приобретения/освобождения блокировки используется менеджер контекста; чтобы избежать излишней нагрузки в цикле ожидания, и для имитации выполнения продолжительных операций добавлен вызов функции sleep""" import _thread as thread, time stdoutmutex = thread.allocate_lock() numthreads = 5 exitmutexes = [thread.allocate() for i in range(numthreads)] def counter(myId, count, mutex): for i in range(count): time.sleep(1 / (myId + 1)) with mutex: print('[%s] => %s' % (myId, i)) exitmutexes[myId].acquire() for i in range(numthreads): thread.start_new_thread(counter, (i, 5, stdoutmutex)) while not all(mutex.locked() for mutex in exitmutexes): time.sleep(0.25) print('Main thread exiting')