def post(self):
json_body = json.loads(self.request.body.decode('utf-8'))
token=json_body['token']
sent = json_body['sent']
if token != config.token:
await self.db.add_log("openid","admin time command",self.request.body.decode('utf-8') +"\n"+"token err")
self.finish('code err')
return
config.timeDelay = -1
await self.notifyTimeServer()
# sent the remaining redpackage
if sent:
print("??????????")
await tornado.gen.sleep(5)
print("?????")
await sentRemaining()
self.finish("remaining package sent")
return
self.finish("OK")
python类gen()的实例源码
def fake_async_decorator(ocls):
"""
Wrap all non-private methods of a class with the tornado Task decorator.
Used to simulate the interface of async methods, but with methods that actually block.
TODO: Test.
"""
import tornado
import tornado.gen
cls = type(ocls.__name__ + 'Async', ocls.__bases__, dict(ocls.__dict__))
for attr in cls.__dict__:
if callable(getattr(cls, attr)) and (not attr.startswith('_')):
def fake(*args, **kw):
yield tornado.gen.Task(getattr(cls, attr)(*args, **kw))
setattr(cls, attr, fake)
return cls
def _server_request_loop(self, delegate):
try:
while True:
conn = CustomHTTP1Connection(self.stream, False,
self.params, self.context)
request_delegate = delegate.start_request(self, conn)
try:
ret = yield conn.read_response(request_delegate)
except (tornado.iostream.StreamClosedError,
tornado.iostream.UnsatisfiableReadError):
return
except tornado.http1connection._QuietException:
# This exception was already logged.
conn.close()
return
except Exception:
tornado.http1connection.gen_log.error("Uncaught exception", exc_info=True)
conn.close()
return
if not ret:
return
yield tornado.gen.moment
finally:
delegate.on_close(self)
def sendRedPackage(self):
await self.db.add_log(self.openid,"try send RedPackage", "retry_Time :" + str(self.retry))
url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"
strr = generateWXParam(self.openid, self.amount)
request = HTTPRequest(url = url, method = "POST", body = strr, client_key="/home/coco/cert/apiclient_key.pem",
ca_certs="/home/coco/cert/rootca.pem", client_cert="/home/coco/cert/apiclient_cert.pem")
client = AsyncHTTPClient()
try:
response = await client.fetch(request)
res = parseWeixin(response.body.decode('utf-8'))
await self.db.add_log(self.openid,"send RedPackage response", res)
if res['return_code'] == 'SUCCESS' and res['result_code']=='SUCCESS' :
config.hasSent[config.turn-1][self.openid] = self.amount/100.0
await self.db.add_order(self.openid,config.turn,self.amount/100.0,"Sent")
else :
config.sendPackageResponseError += 1
await self.db.add_order(self.openid,config.turn,self.amount/100.0,"NotSent")
except Exception as e:
await self.db.add_log(self.openid,"send RedPackage response", "redpackage Callback failed")
config.sendPackageError += 1
if(self.retry>1):
return
self.retry += 1;
delay_time = self.retry*6
await self.db.add_log(self.openid,"send RedPackage retry", "start sleep: " + delay_time)
await tornado.gen.sleep(delay_time)
await self.db.add_log(self.openid,"send RedPackage retry", "end sleep")
await self.sendRedPackage()
def execute_job(self):
mode = self.get_argument("mode")
message = self.get_argument("msg")
thumb = self.get_argument("thumb")
# load setting
pos = config.images_dict[mode][2]
font_size = config.images_dict[mode][3]
font_color = config.images_dict[mode][4]
filename = config.image_path + config.images_dict[mode][5]
# gen picture
img = Image.open(filename)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(config.font_path, font_size)
imgW, imgH = img.size
textW, textH = draw.textsize(message,font=font)
# print(textW, textH)
draw.text(((imgW-textW)/2,pos),message,font_color,font=font)
# thumb
if thumb == "1":
img.thumbnail((300,300),Image.ANTIALIAS)
# gen file pointer
img_io = BytesIO()
img.save(img_io, 'JPEG', quality=100)
img_io.seek(0)
# return picture
return img_io.getvalue()