def __init__(self, conf_ctx, ctx_store, init_type='INIT_TYPE_METHOD', init_job_seq=None, event_listener_list_syn=True, other_info={}): ''' shcedule the task according to the events, providing event listener registry interface, and maintaining the event generator, the event listener list, the global id generator and the context store params: ctx_store: the context store current task scheduler using conf_ctx: a context containing the configure data for this task scheduler, consists of: 'job_unit_sequences': (job_unit_sequence_name): job_unit_sequence 'processed_event_types': (event_type_name): event type description 'global_event_producers': (event_producer_name): event producer description init_type: indicating how to initiate this task scheduler, by using a initiating job unit sequence or the initiating method event_listener_list_syn: indicating whether the event listener list need to by multi-thread protected other_info: the other info given in the configure file injected by the configurer ''' self.event_listener_list = [] # event listener list self.event_listener_list_mutex = None if event_listener_list_syn: self.event_listener_list_mutex = lock( ) # the mutex for event listener list operations self.context_store = ctx_store # context store self.global_id_generator = GlobalIdGenerator() # global id generator self.event_generator = EventGenerator( self.event_listener_list, self.event_listener_list_mutex, self.global_id_generator) # event generator self.config_context = conf_ctx self.task_thread_pool = threadpool.ThreadPool(TASK_THREADPOOL_SIZE) self.task_thread_pool_mutex = lock() self.task_shared_thread_pool = threadpool.ThreadPool( TASK_SHARED_THREADPOOL_SIZE) self.task_shared_thread_pool_mutex = lock() self.init_job_unit_sequence = init_job_seq self.other_info = other_info self._init(init_type)
def __init__(self, task_sch, job_unit_seq, event=None, thread_pool=None, thread_pool_mutex=None, **params): ''' task unit responding to execute a job unit sequence params: task_sch: the task scheduler instance this task unit belongs to job_unit_seq: job unit sequence for this task unit event: the event trigger the creation of this task unit thread_pool: the thread pool for the task unit to execute jobs thread_pool_mutex: the mutex for the tread pool params: other parameters may used for extension ''' self.task_scheduler = task_sch self.origin_job_unit_sequence = job_unit_seq self.remain_job_unit_name_sequence = job_unit_seq.get_copy_job_unit_name_sequence( ) self.remain_params_mapping_sequence = job_unit_seq.get_copy_param_mapping_sequence( ) self.triggered_event = event self.thread_pool = thread_pool self.thread_pool_mutex = thread_pool_mutex if not thread_pool: self.thread_pool = threadpool.ThreadPool(THREADPOOL_SIZE) if not thread_pool_mutex: self.thread_pool_mutex = lock() self.event_listener_info = {} self.event_listener_info_mutex = lock() self.current_job_unit_list = [ ] # the job unit instance list of current phase self.current_job_unit_list_mutex = lock() self.output_params = {} ''' a dict to store the parameters output by the job unit the dict consists with: phase: job_name: para_name: ''' self.output_params_mutex = lock()
def __init__(self, e_id, e_type, e_producer, e_listener_list, e_listener_list_mutex, thread_pool, e_data={}): ''' params: e_id: event id e_type: event type e_producer: event producer e_listener_list: event listener list e_listener_list_mutex: event listener list mutex thread_pool: thread pool for event triggering e_data: data of event, e.g. rest api name and post data ''' self.id = e_id self.type = e_type self.producer = e_producer self.meta = {'event_id': self.id,\ 'event_type': self.type,\ 'event_producer': self.producer} self.data = e_data self.trigger = EventTrigger(self, e_listener_list, e_listener_list_mutex, thread_pool) self.return_flag = False self.return_data = None self.return_data_mutex = lock()
def delete(self, key): with lock(): for p in self.peer['prefixes']: if p['prefix'] == key: self.peer['prefixes'].remove(p) self.db['peers'].save(self.peer) break
def get(self,key): with lock(): cursor = self.db.cursor() cursor.execute('''select * from ''' + self.name + ''' where prefix = ?''', (key,)) return cursor.fetchone()
def get_prefixes(self, key): with lock(): result = [] for p in self.peer['prefixes']: if p['prefix'] == key: result.append(p) return result
def delete_neighbor(self, key, neighbor): with lock(): for p in self.peer['prefixes']: if p['prefix'] == key and p['next_hop'] == neighbor: self.peer['prefix'].remove(p) self.db['peers'].save(self.peer) break
def __init__(self): ''' a storage used to store the component context persistent storage could be added in future ''' self.root_context = {} ''' root context dict, contains the context item list as the value, and the context namespace the context item list belongs to as the key ''' self.context_item_ids = { } # a dict contains all the context item ids under each namespace self.root_context_locator = {} ''' root context locator list, contains the context locator which is calculated by both the context namespace and the item id, at the same position where the context is stored in the context item list under the context namespace ''' self.root_mutex = lock( ) # the mutex for append or pop element in root context list self.context_mutexs = {} '''
def add_many(self,items): with lock(): cursor = self.db.cursor() if (isinstance(items,list)): cursor.execute('''insert into ''' + self.name + ''' (prefix, next_hop, origin, as_path, med, atomic_aggregate) values(?,?,?,?,?,?)''', items)
def update(self, key, item, value): with lock(): cursor = self.db.cursor() script = "update " + self.name + " set " + item + " = '" + value + "' where prefix = '" + key + "'" cursor.execute(script)
def update(self,key,item,value): with lock(): cursor = self.db.cursor() script = "update " + self.name + " set " + item + " = '" + value + "' where prefix = '" + key + "'" cursor.execute(script)
def add_many(self,items): with lock(): cursor = self.db.cursor() if (isinstance(items,list)): cursor.execute('''insert or replace into ''' + self.name + ''' (prefix, next_hop, origin, as_path, communities, med, atomic_aggregate) values(?,?,?,?,?,?,?)''', items)
def print_lock(self): """ TODO: create a new print lock :return: the lock created """ # your code here. write_lock = threading.lock() return write_lock # modify the return to return a the lock created
def delete(self,key): with lock(): # TODO: Add more granularity in the delete process i.e., instead of just prefix, # it should be based on a conjunction of other attributes too. cursor = self.db.cursor() cursor.execute('''delete from ''' + self.name + ''' where prefix = ?''', (key,))
def filter(self,item,value): with lock(): cursor = self.db.cursor() script = "select * from " + self.name + " where " + item + " = '" + value + "'" cursor.execute(script) return cursor.fetchall()
def update_neighbor(self, key, item): with lock(): if (isinstance(item, dict)): for p in self.peer['prefixes']: if p['prefix'] == key and p['next_hop'] == item['next_hop']: self.peer['prefixes'].remove(p) break item['prefix'] = key self.peer['prefixes'].append(item) self.db['peers'].save(self.peer)
def filter(self, item, value): with lock(): cursor = self.db.cursor() script = "select * from " + self.name + " where " + item + " = '" + value + "'" cursor.execute(script) return cursor.fetchall()
def __init__(self, import_name, static_url_path=none, static_folder='static', template_folder='templates', instance_path=none, instance_relative_config=false): _packageboundobject.__init__(self, import_name, template_folder=template_folder) if static_url_path is not none: self.static_url_path = static_url_path if static_folder is not none: self.static_folder = static_folder if instance_path is none: instance_path = self.auto_find_instance_path() elif not os.path.isabs(instance_path): raise valueerror('if an instance path is provided it must be ' 'absolute. a relative path was given instead.') self.instance_path = instance_path self.config = self.make_config(instance_relative_config) self._logger = none self.logger_name = self.import_name self.view_functions = {} self.error_handler_spec = {none: self._error_handlers} self.url_build_error_handlers = [] self.before_request_funcs = {} self.before_first_request_funcs = [] self.after_request_funcs = {} self.teardown_request_funcs = {} self.teardown_appcontext_funcs = [] self.url_value_preprocessors = {} self.url_default_functions = {} self.template_context_processors = { none: [_default_template_ctx_processor] } self.blueprints = {} self.extensions = {} self.url_map = map() self._got_first_request = false self._before_request_lock = lock() if self.has_static_folder: self.add_url_rule(self.static_url_path + '/<path:filename>', endpoint='static', view_func=self.send_static_file)
def get_all(self,key=None): with lock(): cursor = self.db.cursor() if (key is not None): cursor.execute('''select * from ''' + self.name + ''' where prefix = ?''', (key,)) else: cursor.execute('''select * from ''' + self.name) return cursor.fetchall()
def update(self, item, value): with lock(): if not self.peer['prefixes']: self.peer['prefixes'] = [] for p in self.peer['prefixes']: if p['prefix'] == item: self.peer['prefixes'].remove(p) break value['prefix'] = item self.peer['prefixes'].append(value) self.db['peers'].save(self.peer)
def setPosWithSpeed(self, target): with threading.lock(): direction = 1 if (self.position >= target): direction = -1 targetLocalCopy = target for pos in range(self.position, (target + direction), direction): if target == targetLocalCopy: self.motorTarget = pos time.sleep(self.speed) else: break
def ssl_certs(): q = Queue() lock = threading.lock() context = ssl.create_default_context() def threader(): while True: ip = q.get() grab(ip, port_num) q.task_done() def grab(ip, port): try: s = socket(AF_INET, SOCK_STREAM) s.settimeout(5) ret = s.connect_ex((ip.rstrip(), port)) if ret == 0: # s.sendall(b'GET / HTTP/1.1\r\n\r\n') # ban = s.recv(4096).decode('utf-8') cert = ssl.get_server_certificate( (ip.rstrip(), port), ssl_version=ssl.PROTOCOL_TLSv1) x509 = OpenSSL.crypto.load_certificate( OpenSSL.crypto.FILETYPE_PEM, cert) cert_info = x509.get_subject().get_components() cert_info = [(t[0].decode('utf-8'), t[1].decode('utf-8')) for t in cert_info] lock.acquire() if cert_info == '': info['empty'].append((ip, cert_info)) else: info['success'].append((ip, cert_info)) lock.release() else: lock.acquire() info['fail'].append((ip, ret)) lock.release() s.close() except Exception as e: lock.acquire() info['except'].append((ip, str(e))) lock.release() s.close() for _ in range(300): t = threading.Thread(target=threader) t.daemon = True t.start() with open(open_file, 'r') as f: ips = json.load(f) ips = ips['open'] for ip in ips: q.put(ip.rstrip()) q.join()
def add(self, key, item): with lock(): if not self.peer['prefixes']: self.peer['prefixes'] = [] if (isinstance(item, dict)): for p in self.peer['prefixes']: if p['prefix'] == key: self.peer['prefixes'].remove(p) break item['prefix'] = key self.peer['prefixes'].append(item) self.db['peers'].save(self.peer)
def init(): global waitlist_queue # running status global is_running global picked_item global waitlist_queue_lock waitlist_queue = [] is_running = False picked_item = None waitlist_queue_lock = threading.lock() Encoder.init()
def add(self,key,item): with lock(): cursor = self.db.cursor() if (isinstance(item,tuple) or isinstance(item,list)): cursor.execute('''insert or replace into ''' + self.name + ''' (prefix, next_hop, origin, as_path, communities, med, atomic_aggregate) values(?,?,?,?,?,?,?)''', (key,item[0],item[1],item[2],item[3],item[4],item[5])) elif (isinstance(item,dict) or isinstance(item,sqlite3.Row)): cursor.execute('''insert or replace into ''' + self.name + ''' (prefix, next_hop, origin, as_path, communities, med, atomic_aggregate) values(?,?,?,?,?,?,?)''', (key,item['next_hop'],item['origin'],item['as_path'],item['communities'],item['med'],item['atomic_aggregate']))
def add(self,key,item): with lock(): cursor = self.db.cursor() if (isinstance(item,tuple) or isinstance(item,list)): cursor.execute('''insert into ''' + self.name + ''' (prefix, next_hop, origin, as_path, med, atomic_aggregate) values(?,?,?,?,?,?)''', (key,item[0],item[1],item[2],item[3],item[4])) elif (isinstance(item,dict) or isinstance(item,sqlite3.Row)): cursor.execute('''insert into ''' + self.name + ''' (prefix, next_hop, origin, as_path, med, atomic_aggregate) values(?,?,?,?,?,?)''', (key,item['next_hop'],item['origin'],item['as_path'],item['med'], item['atomic_aggregate']))
def update_many(self,key,item): with lock(): cursor = self.db.cursor() if (isinstance(item,tuple) or isinstance(item,list)): cursor.execute('''update ''' + self.name + ''' set next_hop = ?, origin = ?, as_path = ?, communities = ?, med = ?, atomic_aggregate = ? where prefix = ?''', (item[0],item[1],item[2],item[3],item[4],item[5],key)) elif (isinstance(item,dict) or isinstance(item,sqlite3.Row)): cursor.execute('''update ''' + self.name + ''' set next_hop = ?, origin = ?, as_path = ?, communities = ?, med = ?, atomic_aggregate = ? where prefix = ?''', (item['next_hop'],item['origin'],item['as_path'],item['communities'],item['med'], item['atomic_aggregate'],key))
def add_context_item(self, ctx, ctx_ns, item_id, item_id_n='context_item_id', is_lock=True): ''' add a context item to the store, the namespace of the added item will be created if it dosen't exist params: ctx: the data of the context item added ctx_ns: the namespace of the added context item item_id: the id of the context item, should be unique in the namespace item_id_n: the name for context item id is_lock: indicating whether add a multi-threading protection for the added context ''' try: ctx_ns_id, item_gid = self._get_store_id(ctx_ns, item_id) if self.root_mutex.acquire(): try: ext_ctx = {item_id_n: str(item_id)} ext_ctx.update(ctx) ns_locators = self.root_context_locator.setdefault( ctx_ns_id, []) if item_gid in ns_locators: i = ns_locators.index(item_gid) self.root_context[ctx_ns_id].pop(i) self.root_context_locator[ctx_ns_id].pop(i) self.context_mutexs[ctx_ns_id].pop(i) self.context_item_ids[ctx_ns].pop(i) self.context_item_ids.setdefault(ctx_ns, []).append(item_id) self.root_context.setdefault(ctx_ns_id, []).append(ext_ctx) self.root_context_locator.setdefault(ctx_ns_id, []).append(item_gid) if is_lock: self.context_mutexs.setdefault(ctx_ns_id, []).append(lock()) else: self.context_mutexs.setdefault(ctx_ns_id, []).append(None) except Exception, e: print Exception, ':', e, \ ' in %s:%s' % get_class_func_name(self) self.root_mutex.release() else: self.root_mutex.release() except Exception, e: print Exception, ':', e, \ ' in %s:%s' % get_class_func_name(self)
class MyLog: log = None mutex = threading.lock() def __init__(self): pass @staticmethod def get_log(): if MyLog.log is None: MyLog.mutex.acquire() MyLog.log = Log() MyLog.mutex.release() return MyLog.log
def update_many(self,key,item): with lock(): cursor = self.db.cursor() if (isinstance(item,tuple) or isinstance(item,list)): cursor.execute('''update ''' + self.name + ''' set next_hop = ?, origin = ?, as_path = ?, med = ?, atomic_aggregate = ? where prefix = ?''', (item[0],item[1],item[2],item[3],item[4],key)) elif (isinstance(item,dict) or isinstance(item,sqlite3.Row)): cursor.execute('''update ''' + self.name + ''' set next_hop = ?, origin = ?, as_path = ?, med = ?, atomic_aggregate = ? where prefix = ?''', (item['next_hop'],item['origin'],item['as_path'],item['med'], item['atomic_aggregate'],key))
def main(num): global count, mutex thread = [] count = 1 #创建锁 mutex = threading.lock() for x in xrange(0, num): threads.append(threading.Thread(target=thread_main, args=(10,))) #启动所有线程 for t in threads: t.start() #主线程等待所有子线程推出 for t in threads: t.join()
def __init__(self,ip,name): with lock(): # Create a database in RAM self.db = sqlite3.connect('/home/vagrant/sdx-ryu/xrs/ribs/'+ip+'.db',check_same_thread=False) self.db.row_factory = sqlite3.Row self.name = name # Get a cursor object cursor = self.db.cursor() cursor.execute(''' create table if not exists '''+self.name+''' (prefix text, next_hop text, origin text, as_path text, communities text, med integer, atomic_aggregate boolean) ''') self.db.commit()
def __init__(self,ip,name): with lock(): # Create a database in RAM self.db = sqlite3.connect(cwd+'/pyretic/sdx/ribs/'+ip+'.db',check_same_thread=False) self.db.row_factory = sqlite3.Row self.name = name # Get a cursor object cursor = self.db.cursor() cursor.execute(''' create table if not exists '''+self.name+''' (prefix text, next_hop text, origin text, as_path text, med integer, atomic_aggregate boolean) ''') self.db.commit()
class MyEmail: """ pass """ email = None mutex = threading.lock() def __init__(self): """ pass """ pass @staticmethod def get_email(): """ pass """ if MyEmail.email is None: MyEmail.mutex.acquire() MyEmail.email = Email() MyEmail.mutex.release() return MyEmail.email
def __init__(self,ip,name): with lock(): # Create a database in RAM base_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)),"ribs")) self.db = sqlite3.connect(base_path+'/'+ip+'.db',check_same_thread=False) self.db.row_factory = sqlite3.Row self.name = name # Get a cursor object cursor = self.db.cursor() cursor.execute(''' create table if not exists '''+self.name+''' (prefix text, next_hop text, origin text, as_path text, communities text, med integer, atomic_aggregate boolean, primary key (prefix)) ''') self.db.commit()
def banners(): q = Queue() lock = threading.lock() def threader(): while True: ip = q.get() grab(ip, port_num) q.task_done() def grab(ip, port): try: s = socket(AF_INET, SOCK_STREAM) s.settimeout(5) ret = s.connect_ex((ip.rstrip(), port)) if ret == 0: s.sendall(b'GET / HTTP/1.1\r\n\r\n') ban = s.recv(4096).decode('utf-8') lock.acquire() if ban == '': info['empty'].append((ip, ban)) else: info['success'].append((ip, ban)) lock.release() else: lock.acquire() info['fail'].append((ip, ret)) lock.release() s.close() except Exception as e: lock.acquire() info['except'].append((ip, str(e))) lock.release() s.close() for _ in range(300): t = threading.Thread(target=threader) t.daemon = True t.start() with open(open_file, 'r') as f: ips = json.load(f) ips = ips['open'] for ip in ips: q.put(ip.rstrip()) q.join()
def __init__(self, sdx_id, names): self.lock = lock() with self.lock: # Create a database in RAM # base_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "ribs")) # self.db = sqlite3.connect(base_path + '/' + str(sdx_id) + '.db', check_same_thread=False) self.db = sqlite3.connect(':memory:', check_same_thread=False) self.db.row_factory = SQLRIB.dict_factory # Get a cursor object cursor = self.db.cursor() for name in names: cursor.execute( 'CREATE TABLE IF NOT EXISTS ' + str(name) + ' (participant INT, prefix TEXT, next_hop TEXT, ' 'origin TEXT, as_path TEXT, communities TEXT, med INT, atomic_aggregate BOOLEAN, ' 'PRIMARY KEY (participant, prefix))') self.db.commit()
def flush(self): lock=threading.lock() while True: lock.acquire() try: proxy=self.curse.next() proxies = {"http": 'http://'+proxy} except StopIteration: print 'not available' break try: r=requests.get('http://www.baidu.com',proxies=proxies,timeout=5) except: self.ips.remove(proxy) print proxy+' Can`t connect!'+' No.'+str(self.content.index(proxy)) continue print r.content self.write(self.ips)
def __init__(self, sdx_id): self.lock = lock() with self.lock: # base_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "cibs")) # self.db = sqlite3.connect(base_path + '/' + str(sdx_id) + '.db', check_same_thread=False) self.db = sqlite3.connect(':memory:', check_same_thread=False) self.db.row_factory = sqlite3.Row # Get a cursor object cursor = self.db.cursor() cursor.execute('CREATE TABLE IF NOT EXISTS input (i_participant INT, prefix TEXT, sender_sdx INT, ' 'sdx_set TEXT, PRIMARY KEY (i_participant, prefix, sender_sdx))') cursor.execute('CREATE TABLE IF NOT EXISTS local (i_participant INT, prefix TEXT, sdx_set TEXT, ' 'PRIMARY KEY (i_participant, prefix))') cursor.execute('CREATE TABLE IF NOT EXISTS output (e_participant INT, prefix TEXT, ' 'receiver_participant INT, sdx_set TEXT, ' 'PRIMARY KEY (e_participant, prefix, receiver_participant))') self.db.commit()
def print_time(threadName, delay, counter): while counter: time.sleep(delay) print("%s: %s" % (threadName, time.ctime(time.time()))) counter -= 1 threadLock = threading.lock() threads = [] #创建新线程 thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) thread1.start() thread2.start() threads.append(thread1) threads.append(thread2) #等待所有线程新线程完成 for t in threads: t.join() print("Exiting Main Thread")
import socket, argparse, sys, random, time, threading lock_send = threading.Lock() lock_recieve = threading.lock() def node_management_recieve(client_sock): global lock while True: while lock.locked(): pass else: with lock: try: client_sock.settimeout(4) message = client_sock.recv(1024) if message: if message == "access": time.sleep(2) client_sock.send("free") else: client_sock.close() print("Servidor saiu") except socket.timeout: client_sock.settimeout(None) def node_management_send(client_sock): if lock.locked(): print "Other node acessing you and processing some data.Try again later" threading.Timer(random.randint(2, 5), node_management_send,
def __init__(self): self.storage = 0 self.__lock = threading.lock()
def get(self, key): with lock(): for p in self.peer['prefixes']: if p['prefix'] == key: return p return None
def get_all(self, key=None): with lock(): if self.peer['prefixes']: return self.peer['prefixes'] else: return None
def __init__(self): super().__init__() lock = threading.lock() pass
def delete_all(self): with lock(): if not self.peer['prefixes']: self.peer['prefixes'] = [] self.peer['prefixes'] = [] self.db['peers'].save(self.peer)
def filter(self, item, value): with lock(): pass
def __init__(self): pass self.dynamic_namespace = {} self.mutex = lock()
def delete_all(self): with lock(): cursor = self.db.cursor() cursor.execute('''delete from ''' + self.name)
def commit(self): with lock(): self.db.commit()
def rollback(self): with lock(): self.db.rollback()
import socket import threading from threading import Semaphore as sem from threading import Lock as lock from threading import Thread as thr import numpy as np from numpy import pi as pi import time import matplotlib.pyplot as plt import sys try: from scipy.integrate import odeint as EDO except: os.system('python -m pip install scipy') mut = lock() maxIn = 10**(1 / 2) qin = maxIn qout = 0 h = [] h.append(0) href = 5 R0 = 2.5 R1 = 5 H = 10 class process_thread(thr): def __init__(self, clientAddress=0, clientsocket=0): self.endCli = clientAddress self.sockCli = clientsocket
print("hello",n) for x in xrange(1,10): t=threading.Thread(target=run,args=("s%",x),) t.setDaemon(True)#设置为守护线程,在start之前 t.start() #等待thread运行使用join,如t.join() threading.crrent_thread #当前线程 threading.active_thread #当前活跃的线程 #守护线程 #线程锁 lock=threading.lock() #lock=threading.Rlock()递归锁 def run(n): lock.acquire() gloabal num num+=1 lock.release() #信号量 #semaphore=threading.BondeSemaphore(5) #最多允许5个线程同时运行 #用法和锁一样 #Events #通过Event来实现两个或多个线程间的交互,下面是一个红绿灯的例子,即起动一个线程做交通指挥灯,生成几个线程做车辆,车辆行驶按红灯停,绿灯行的规则。 import threading,time
def __init__(self, ip): with lock(): # Connect on MongoDB and access database self.db = MongoClient(MONGO_ADDRESS)[MONGO_DB_NAME] self.peer = self.db['peers'].find_one({'IP': ip})
import threading message_lock = threading.lock() with message_lock: messages.add(newmessage)
yield request, self, RV.Channel if RV.Channel.waitQ != []: Node.WastedSlots += 1 self.Flag = 1 print self.ID, self.Flag yield release, self, RV.Channel else: Node.NonWastedSlots += 1 self.Packets -= 1 self.Flag = 0 yield hold, self, (now() + 1) yield release, self, RV.Channel MutexLock = threading.lock() totalHosts = [] for Nodes in range(8): Host = Node(Nodes,MutexLock) Host.start() totalHosts.append(Host) for host in totalHosts: host.join() def PacketTransmit(): while True: packetPID = os.fork() if packetPID == 0: packetSend()
def __del__(self): with lock(): self.db.close()