def request_sys(req_url, request_data, method, reqheaders): logger.info("request_data is {0}".format(request_data)) logger.info("headers is {0}".format(reqheaders)) try: if 'GET' == method: # request_data = urllib.urlencode(request_data).encode(encoding='utf-8') result = requests.get(url=req_url, params=request_data, headers=reqheaders) # request = urllib2.Request(url="{0}?{1}".format(req_url, request_data), headers=reqheaders) # res = urllib2.urlopen(request) # result = res.read() logger.info("res content is {0}".format(result)) return json.loads(result) elif 'POST' == method: reqheaders['Accept'] = 'application/json' if reqheaders.get('Content-Type') == 'application/json': request_data = json.dumps(request_data, cls=APIEncoder) result = requests.post(url=req_url, data=request_data, headers=reqheaders).content logger.info("res content is {0}".format(result)) return json.loads(result) else: logger.info("method error, current method is {0}".format(method)) except Exception as e: logger.error('request_order_sys access error:%s' % (traceback.format_exc(e), )) return None
def is_thread_finish(thread_pool): while True: for t in thread_pool: if not t.isAlive(): # logger.info("t{0} is finished".format(t.getName())) thread_pool.pop(thread_pool.index(t)) if not thread_pool: break logger.info("all thread finish")
def multi_process(func, fun_type=""): logger.info("multi_process type is {0}".format(fun_type)) process_list = [] for x in range(3): p = Process(target=func, args=()) process_list.append(p) p.start() e = process_list.__len__() while True: for pr in process_list: if not pr.is_alive(): e -= 1 if e <= 0: break
def run(): thread_pool = list() for i in range(5): t = Thread(target=action, args=[i]) # t.setDaemon(True) thread_pool.append(t) t.start() while True: for t in thread_pool: if not t.isAlive(): logger.info("t{0} is finished".format(t.getName())) thread_pool.pop(thread_pool.index(t)) if not thread_pool: break
def _max_connect(*args, **kwargs): _max = 5 connect = 0 e = None while connect < _max: try: func(*args, **kwargs) except Exception as e: logger.error(repr(e)) connect += 1 logger.error("connect is {0}".format(connect)) else: break if connect == _max: logger.info(func.__name__) logger.info("finally error is {0}".format(repr(e)))
def init_data_order_vehicles(order_number): plate_type = [u"6.8米", u"7.6米", u"9.6米", u"14.5米", u"17.5米"] url = "http://172.16.7.47:5001/v1/order_vehicles/dispatch/vehicle" re_data = { "order_number": str(order_number), "driver_name": u"小王", "driver_telephone": "13221124493", "antipate_arrive_time": str(datetime.now() + timedelta(hours=2))[:16], "vehicle_type": random.choice(plate_type) } truck_info = { "plate": u"沪C88888", "carriage_plate": u"", "carriage_length_type": "9.6", "carriage_type": u"高栏车", "send_vehicle_type": "1", } reqheaders = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko', 'Content-Type': 'application/json', 'token': "131ccd7e6e979858f77819c42a1e94d1", "shippersid": 203 } re = request_sys(url, dict(re_data.items() + truck_info.items()), "POST", reqheaders) if not re or re['code'] != 1000: logger.info("result is {0}".format(re)) truck_info = { "plate": u"陕Q12349", "carriage_plate": u"川HQ8092", "carriage_length_type": "9.6", "carriage_type": u"厢式车", "send_vehicle_type": "2", } re = request_sys(url, dict(re_data.items() + truck_info.items()), "POST", reqheaders) if not re or re['code'] != 1000: logger.info("result is {0}".format(re))
def inin_data_order(): url = "http://172.16.7.47:5001/v1/orders/create/order" re_data = { "line_no": "MOT000004TO00000101", "carrier_code": "203", "antipate_arrive_time": (datetime.now() + timedelta(hours=5)), "demand_carriage_type": "XS", "order_demand": [{ "carriage_length_type": u"6.8米", "vehicle_amount": 2 }, { "carriage_length_type": u"7.6米", "vehicle_amount": 2 }, { "carriage_length_type": u"9.6米", "vehicle_amount": 2 }, { "carriage_length_type": u"14.5米", "vehicle_amount": 2 }, { "carriage_length_type": u"17.5米", "vehicle_amount": 2 }] } settlement_type = random.choice(['ZC', 'JZ']) re_data['settlement_type'] = settlement_type if settlement_type == "JZ": re_data['require_total_volume'] = random.randint(50, 300) else: re_data['require_total_volume'] = "" reqheaders = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko', 'Content-Type': 'application/json', 'token': "ST-1269-ghei4VdqVEb6yQZGsbti-cas01.example.org" } re = request_sys(url, re_data, "POST", reqheaders) if not re or re['code'] != 1000: logger.info("result is {0}".format(re))
def action(arg): time.sleep(1) logger.info("sub Thread start! the thread name is {0}".format( currentThread().getName())) logger.info("the arg is {0}".format(arg)) time.sleep(1)
def action(arg): time.sleep(1) logger.info("sub Thread start! the thread name is {0}".format( currentThread().getName())) logger.info("the arg is {0}".format(arg)) time.sleep(1) @get_func_time def run(): thread_pool = list() for i in range(5): t = Thread(target=action, args=[i]) # t.setDaemon(True) thread_pool.append(t) t.start() while True: for t in thread_pool: if not t.isAlive(): logger.info("t{0} is finished".format(t.getName())) thread_pool.pop(thread_pool.index(t)) if not thread_pool: break if __name__ == '__main__': logger.info("main_thread start") run()
def _get_func_time(*args, **kwargs): now1 = datetime.now() func(*args, **kwargs) now2 = datetime.now() logger.info("func is {0}, time spending is {1}".format( func.__name__, (now2 - now1).total_seconds()))