def parse_file(file): conn = pymongo.MongoClient("mongodb://localhost:27017/") iboss = conn["iboss"] interface = iboss["interface_log"] with open(file, 'r',encoding="utf-8") as f: line_list = [] for line in f: res = json.loads(str(line[line.find('{'):])) line_list.append(res) if(len(line_list) == 1000): x = interface.insert_many(line_list) print(threading.current_thread().getName(),x.inserted_ids) line_list = [] for fpathe,dirs,fs in os.walk("C:/DATA/crmDetail_689186.log.201808_log_bak/"): for ff in fs: file = os.path.join(fpathe,ff) t=threading.Thread(target=parse_file,args=(file,)) t.start() while 1: pass
def main(): hostname = socket.gethostname() zabbix = ZabbixSender(zabbix_server=config['zabbix_server']) while True: packets = [] while stats_queue.empty() is False: disk, data = stats_queue.get() packets.extend( [ZabbixMetric(hostname, f'iostat[{disk},{key}]', value) for key, value in data.items()]) if packets: result = zabbix.send(packets) print(packets, result) time.sleep(1) if __name__ == '__main__': signal.signal(signal.SIGINT, signal_handler) path = 'iostat.yml' with open(path, 'r') as config_file: config = yaml.load(config_file, Loader=yaml.Loader) t = threading.Thread(target=producer) t.daemon = True t.start() # del t main()
def multi_thread_save_feat(imglist, score, save_root): for idx, line in enumerate(imglist): new_name = line.split(' ')[0].split('.jpg')[0] save_name = save_root + new_name + '.npy' os.makedirs(os.path.dirname(save_name), exist_ok=True) np.save(save_name, score[idx]) correct_score = new_score[:len(lines)] n_threads = 40 threads = [] length = len(lines) // n_threads for i in range(n_threads): start = i * length if i == n_threads - 1: end = len(lines) else: end = start + length t = threading.Thread(target=multi_thread_save_feat, args=(lines[start:end], correct_score[start:end], save_root)) threads.append(t) for i in range(n_threads): threads[i].start() # wait for all threads to finish for i in range(n_threads): threads[i].join() print('Completed!', flush=True)
def News_start(self,MainWindow): thread=threading.Thread(target=self.News,args=(self,)) thread.daemon=True #프로그램 종료시 프로세스도 함께 종료 (백그라운드 재생 X) thread.start()
def video_thread(self,MainWindow): thread=threading.Thread(target=self.Video_to_frame,args=(self,)) thread.daemon=True #프로그램 종료시 프로세스도 함께 종료 (백그라운드 재생 X) thread.start()
if __name__ == '__main__': num = 5 start_time = time.clock() count("计算器A",num,1) count("计算器B", num, 1) end_time = time.clock() run_time = end_time-start_time print("总耗时:%0.7f秒" %(run_time)) #总耗时:0.0011839秒 # 线程的方式 if __name__ == '__main__': num = 5 # 创建一个线程( 函数) xianc_a = threading.Thread(count,("计算器A", num, 1)) xianc_b = threading.Thread(count2, ("计算器B", num, 1)) start_time = time.clock() xianc_a.start() xianc_b.start() xianc_a.join() xianc_b.join() end_time = time.clock() run_time = end_time - start_time print("总耗时:%0.7f秒" % (run_time)) #总耗时:0.0010752秒
import threading import time def threadings(num): number = str(num) print ("thread " + number) time.sleep(1) if __name__ == "__main__": threads = [] for i in range(0,5): thread = threading.Thread(target=threadings(i)) threads.append(thread) for j in threads: j.start() print "im dead inside" for j in threads: j.join() print "Im so done" num = 5 threadings(num) print("Hello orld")
if self.searcher.find(content): result=self.searcher.search(content) if result>=0 : return True return True def handle_message(self, callback): callback('abc') # self.simsimi.talk(self.content, callback) if __name__ == "__main__": import threading,time simsimi = SimSimiTalk() def callback(response): print response simsimi.http.stop() def talk(): while 1: if simsimi.ready: simsimi.talk("nice to meet you", callback) break else: time.sleep(1) t = threading.Thread(target = talk) t.setDaemon(True) t.start() simsimi.http.start()
def stop(self, reason=""): self.stop_thread = threading.Thread(target=self.do_stop, args=(reason, )) self.stop_thread.start()
def msg_callback(self, ch, method, properties, body): print(" [x] Received a task msg ") thread = threading.Thread(target=self.start_thread, args=(body, )) thread.start() print(" [x] Received %r" % (body, ))
def __init__( self, port, interval=1, back_seconds=60 * 60, max_records=60 * 24, cleanup_interval=60, disk_cache=None, event_loop=None): """ port Port on which to serve websocket connections interval How frequently to serve updates back_seconds How many seconds of back data to retain max_records Maximum number of records to store for each variable. cleanup_interval How many seconds between calls to cleanup old cache entries and save to disk (if disk_cache is specified) disk_cache If not None, name of directory in which to backup values from in-memory cache event_loop If not None, the event loop to use for websocket events """ self.port = port self.interval = interval self.back_seconds = back_seconds self.max_records = max_records self.cleanup_interval = cleanup_interval self.event_loop = event_loop self.cache = RecordCache() # If they've given us the name of a disk cache, try loading our # RecordCache from it. self.disk_cache = disk_cache if disk_cache: self.cache.load_from_disk(disk_cache) # List where we'll store our websocket connections so that we can # keep track of which are still open, and signal them to close # when we're done. self._connections = [] self._connection_lock = threading.Lock() # If we've received an event loop, use it, otherwise create a new one # of our own. if not event_loop: self.event_loop = asyncio.new_event_loop() asyncio.set_event_loop(self.event_loop) else: self.event_loop = None asyncio.set_event_loop(event_loop) self.quit_flag = False # Start a thread to loop through, cleaning up the cache and (if we've # been given a disk_cache file) backing memory up to it. threading.Thread(target=self.cleanup_loop, daemon=True).start() # Fire up the thread that's going to the websocket server in our # event loop. Calling quit() it will close any remaining # connections and stop the event loop, terminating the server. self.server_thread = threading.Thread( target=self._run_websocket_server, daemon=True) self.server_thread.start()
threading: 高级,对_thread进行封装 任何一个进程都会默认启动一个线程,这个线程称为主线程 ''' import threading,os,time def get(): print('haha---{}--{}'.format(threading.current_thread(),threading.current_thread().name)) if __name__ =='__main__': print('主进程启动,{}'.format(os.getpid())) print('主线程--{}'.format(threading.current_thread())) #创建子线程 p = threading.Thread(target=get,name='th1') q = threading.Thread(target=get,name='th2') p.start() q.start() #最好都等待线程结束 p.join() q.join() print('主线程结束')
def _add_wait(): global timer_th with th_lock: if not timer_th: timer_th = threading.Thread(target=timer_thread) timer_th.start()
import threading #创建一个全局的ThreadLocal对象 #每个线程有独立的存储空间 #每个线程对ThreadLocal对象都可以读写,但是互不影响 #作用:为每个线程绑定一个数据库链接,HTTP请求,用户身份信息等 #这样一个线程的所有调用到的处理函数都可以非常方便地访问这些资源 local = threading.local() def add(a, b): a += b return a def run(num): local.x = num for i in range(5): local.x = add(local.x, i) print("子进程%s: local.x = %d" % (threading.current_thread().name, local.x)) if __name__ == "__main__": thread1 = threading.Thread(target=run, args=(1, )) thread2 = threading.Thread(target=run, args=(250, )) thread1.start() thread2.start() thread1.join() thread2.join()
def main(): start_time = time.time() args = parse_args() if args.verbose > 1: websocket.enableTrace(True) options = {} if args.proxy: p = urlparse(args.proxy) options["http_proxy_host"] = p.hostname options["http_proxy_port"] = p.port if args.origin: options["origin"] = args.origin if args.subprotocols: options["subprotocols"] = args.subprotocols opts = {} if args.nocert: opts = {"cert_reqs": ssl.CERT_NONE, "check_hostname": False} if args.headers: options['header'] = map(str.strip, args.headers.split(',')) ws = websocket.create_connection(args.url, sslopt=opts, **options) if args.raw: console = NonInteractive() else: console = InteractiveConsole() print("Press Ctrl+C to quit") def recv(): try: frame = ws.recv_frame() except websocket.WebSocketException: return websocket.ABNF.OPCODE_CLOSE, None if not frame: raise websocket.WebSocketException("Not a valid frame %s" % frame) elif frame.opcode in OPCODE_DATA: return frame.opcode, frame.data elif frame.opcode == websocket.ABNF.OPCODE_CLOSE: ws.send_close() return frame.opcode, None elif frame.opcode == websocket.ABNF.OPCODE_PING: ws.pong(frame.data) return frame.opcode, frame.data return frame.opcode, frame.data def recv_ws(): while True: opcode, data = recv() msg = None if six.PY3 and opcode == websocket.ABNF.OPCODE_TEXT and isinstance( data, bytes): data = str(data, "utf-8") if not args.verbose and opcode in OPCODE_DATA: msg = data elif args.verbose: msg = "%s: %s" % (websocket.ABNF.OPCODE_MAP.get(opcode), data) if msg is not None: if args.timings: console.write(str(time.time() - start_time) + ": " + msg) else: console.write(msg) if opcode == websocket.ABNF.OPCODE_CLOSE: break thread = threading.Thread(target=recv_ws) thread.daemon = True thread.start() if args.text: ws.send(args.text) while True: try: message = console.read() ws.send(message) except KeyboardInterrupt: return except EOFError: time.sleep(args.eof_wait) return
def cmd_processor(self): while self.running: data = self.get_cmd_data() if not data: break cmd_id = struct.unpack("<B", data.get(1))[0] if cmd_id == 1: # data self.send_to_sock(data) elif cmd_id == 3: # ack: position = struct.unpack("<Q", data.get(8))[0] self.xlog.debug("Conn session:%s conn:%d ACK:%d", self.session.session_id, self.conn_id, position) if position > self.remote_acked_position: self.remote_acked_position = position self.recv_notice.acquire() self.recv_notice.notify() self.recv_notice.release() elif cmd_id == 2: # Closed self.xlog.info("Conn session:%s conn:%d Peer Close:%s", self.session.session_id, self.conn_id, data.get()) if self.is_client: self.transfer_peer_close("finish") self.stop("peer close") elif cmd_id == 0: # Create connect if self.port or len( self.host) or self.next_cmd_seq != 1 or self.sock: raise Exception( "put_send_data %s conn:%d Create but host:%s port:%d next seq:%d" % (self.session.session_id, self.conn_id, self.host, self.port, self.next_cmd_seq)) self.sock_type = struct.unpack("<B", data.get(1))[0] host_len = struct.unpack("<H", data.get(2))[0] self.host = data.get(host_len) self.port = struct.unpack("<H", data.get(2))[0] sock, res = self.do_connect(self.host, self.port) if res is False: self.xlog.debug( "Conn session:%s conn:%d %s:%d Create fail", self.session.session_id, self.conn_id, self.host, self.port) self.transfer_peer_close("connect fail") else: self.xlog.info("Conn session:%s conn:%d %s:%d", self.session.session_id, self.conn_id, self.host, self.port) self.sock = sock self.recv_thread = threading.Thread( target=self.recv_worker) self.recv_thread.start() else: self.xlog.error("Conn session:%s conn:%d unknown cmd_id:%d", self.session.session_id, self.conn_id, cmd_id) raise Exception("put_send_data unknown cmd_id:%d" % cmd_id)
The format includes the time stamp, the thread name, the logging level and the message logged. """ logger = logging.getLogger("threading_example") logger.setLevel(logging.DEBUG) fh = logging.FileHandler("threading.log") fmt = '%(asctime)s - %(threadName)s - %(levelname)s - %(message)s' formatter = logging.Formatter(fmt) fh.setFormatter(formatter) logger.addHandler(fh) return logger def doubler(number, logger): # we pass the logger to the function so we have only one logging object during runtime """ A function that can be used by a thread """ logger.debug('doubler function executing') # instead of printing, we log the statements result = number * 2 logger.debug('doubler function ended with: {}'.format(result)) if __name__ == '__main__': logger = get_logger() thread_names = ['Mike', 'George', 'Wanda', 'Dingbat', 'Nina'] for i in range(5): my_thread = threading.Thread( target=doubler, name=thread_names[i], args=(i,logger)) my_thread.start()
def listen(self, timeout=10): """ Listen for incoming messages. Timeout is used to check if the server must be switched off. :param timeout: Socket Timeout in seconds """ self._socket.settimeout(float(timeout)) while not self.stopped.isSet(): try: data, client_address = self._socket.recvfrom(4096) if len(client_address) > 2: client_address = (client_address[0], client_address[1]) except socket.timeout: continue try: serializer = Serializer() message = serializer.deserialize(data, client_address) if isinstance(message, int): logger.error("receive_datagram - BAD REQUEST") rst = Message() rst.destination = client_address rst.type = defines.Types["RST"] rst.code = message rst.mid = self._messageLayer._current_mid self._messageLayer._current_mid += 1 % 65535 self.send_datagram(rst) continue logger.debug("receive_datagram - " + str(message)) if isinstance(message, Request): #logger.debug("send_datagram - " + str(message)) #print("ddddddddddddddd: " + str(message)) transaction = self._messageLayer.receive_request(message) if transaction.request.duplicated and transaction.completed: logger.debug( "message duplicated, transaction completed") if transaction.response is not None: self.send_datagram(transaction.response) continue elif transaction.request.duplicated and not transaction.completed: logger.debug( "message duplicated, transaction NOT completed") self._send_ack(transaction) continue args = (transaction, ) t = threading.Thread(target=self.receive_request, args=args) t.start() # self.receive_datagram(data, client_address) elif isinstance(message, Response): logger.error("Received response from %s", message.source) else: # is Message transaction = self._messageLayer.receive_empty(message) if transaction is not None: with transaction: self._blockLayer.receive_empty( message, transaction) self._observeLayer.receive_empty( message, transaction) except RuntimeError: print "Exception with Executor" self._socket.close()
print 'clear the previous paths you just ran' self.state_action[:] = [] print 'you can start recording the path from scratch' elif ch == '\x03' or ch == '\x71': # ctrl + c or 'q' rospy.signal_shutdown(ch) sys.exit() else: print ord(ch) if __name__ == "__main__": LfD = LfD() print '===================================================' print 'please press the following keys to control the car:' print ' A: left_45_forward' print ' D: right_45_forward' print ' Up arrow: forward' print ' Down arrow: backward' print ' Left arrow: left_45_backward' print ' Right arrow: right_45_backward' print ' R: reset' print ' Space: stop' print ' S: save path you just ran (This doesn\'t save path to file)' print ' C: clear saved paths' print ' Z: save paths to file' print ' Q: quit' print '===================================================' key_thread = threading.Thread(name="key", target=LfD.read_key) key_thread.start() LfD.env.plt_show()
def __init__(self, server_address, multicast=False, starting_mid=None): """ Initialize the server. :param server_address: Server address for incoming connections :param multicast: if the ip is a multicast address :param starting_mid: used for testing purposes """ self.stopped = threading.Event() self.stopped.clear() self.to_be_stopped = [] self.purge = threading.Thread(target=self.purge) self.purge.start() self._messageLayer = MessageLayer(starting_mid) self._blockLayer = BlockLayer() self._observeLayer = ObserveLayer() self._requestLayer = RequestLayer(self) self.resourceLayer = ResourceLayer(self) # Resource directory root = Resource('root', self, visible=False, observable=False, allow_children=True) root.path = '/' self.root = Tree() self.root["/"] = root self._serializer = None self.server_address = server_address self.multicast = multicast addrinfo = socket.getaddrinfo(self.server_address[0], None)[0] if self.multicast: # pragma: no cover # Create a socket self._socket = socket.socket(addrinfo[1], socket.SOCK_DGRAM) # Allow multiple copies of this program on one machine # (not strictly needed) self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Bind it to the port self._socket.bind(('', self.server_address[1])) group_bin = socket.inet_pton(addrinfo[1], addrinfo[4][0]) # Join group if addrinfo[0] == socket.AF_INET: # IPv4 mreq = group_bin + struct.pack('=I', socket.INADDR_ANY) self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) else: mreq = group_bin + struct.pack('@I', 0) self._socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq) else: if addrinfo[0] == socket.AF_INET: # IPv4 self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) else: self._socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._socket.bind(self.server_address)
def weather_start(self,MainWindow): thread=threading.Thread(target=self.weather_icon,args=(self,)) thread.daemon=True #프로그램 종료시 프로세스도 함께 종료 (백그라운드 재생 X) thread.start()
def start(): global t # create thread and start it t = threading.Thread(target=compute_metrics) t.start()
def meal_start(self,MainWindow): thread=threading.Thread(target=self.School_meal,args=(self,)) thread.daemon=True #프로그램 종료시 프로세스도 함께 종료 (백그라운드 재생 X) thread.start()
assert m_reserved == unpacked[5] assert m_reserved2 == unpacked[6] assert sendObj def test_robotserver(): server = robotServer('10.10.105.184', 19204) server.start() def test_robotclient(): client1= robotClient() client1.connect('10.10.105.184', 19204) data = {"x": 10.0, "y": 3.0, "angle": 0} # 发送数据 sendData = json.dumps(data).encode() client1.send(sendData) client1.disconnect() if __name__ == '__main__': thread1 = threading.Thread(target=test_robotserver) thread1.start() test_protocolHeader() # while(True): # # thread2 = threading.Thread(target=test_robotclient) # thread2.start() # # time.sleep(3)
def loopind(): while True: t = threading.Thread(target=loop()) t.start()
def start(self): if self.thread is None: self.thread = threading.Thread(target=self.server.start) self.thread.daemon = True self.thread.start()
details = " ".join(msgList[2:]) if (details != ""): for cID in clientele: if cID != senderID: sendMsg = instruction + " " + senderID + " " + details + "\n" clientele[cID].send(sendMsg.encode()) print("> sent to %s:" % cID, sendMsg[:-1]) print() serverChannel.task_done() clientele = dict() playerNum = 0 serverChannel = Queue(100) threading.Thread(target=serverThread, args=(clientele, serverChannel)).start() names = ["Rohan", "Eddie", "Kim", "Tara"] while True: client, address = server.accept() # myID is the key to the client in the clientele dictionary myID = names[playerNum] print(myID, playerNum) for cID in clientele: print(repr(cID), repr(playerNum)) clientele[cID].send(("newPlayer %s\n" % myID).encode()) client.send(("newPlayer %s\n" % cID).encode()) clientele[myID] = client client.send(("myIDis %s \n" % myID).encode()) print("connection recieved from %s" % myID)
def run_receiver_thread(dev, num): import threading t = threading.Thread(target=receiver_thread, args=(dev, num)) t.start()
event.set() # 初始值为绿灯 while True: if 5 < count <= 10: event.clear() # 红灯,清除标志位 print("\33[41;1mred light is on...\033[0m") elif count > 10: event.set() # 绿灯,设置标志位 count = 0 else: print("\33[42;1mgreen light is on...\033[0m") time.sleep(1) count += 1 def car(name): while True: if event.is_set(): # 判断是否设置了标志位 print("[%s] running..." % name) time.sleep(1) else: print("[%s] sees red light,waiting..." % name) event.wait() print("[%s] green light is on,start going..." % name) light = threading.Thread(target=lighter, ) light.start() car = threading.Thread(target=car, args=("MINI", )) car.start()
def getPosluh(self): my_thread = threading.Thread(target=self.Listening_test) my_thread.start()