celery变量共享

2020年6月21日 09:19

问题

 
很多情况下我们想让task共享变量,该怎么做?
 

celery的并发原理

 
celery的并发任务池,有eventlet, gevent, prefork, thread类型
 
eventlet/gevent协程: 只有一个进程一个线程, 全局变量在task之间共享
    
prefork属于multiprocessing: multiprocessing全局变量也是共享的
 
thread多线程: 全局变量共享
    

 验证方法

 
用ab命令模拟大量并发,很容易测试出来
 
ab -n 1000 -c 100 -p ./post.txt -T application/json http://xxxx:5000/xxx
 

 结论

 
1. celery如果访问数据库, gpu等资源, 不用担心多次加载
 
2. 注意: 如果在task中初始化全局变量, 初始化较慢, 同时又收到大量task请求,可能会导致初始化多次
    
 
 

Tags: celery
评论(109) 阅读(3156)

protobuf序列化numpy

2020年6月15日 15:02

 

说明

 
protobuf处理不能直接处理numpy,需要先把numpy转为字节
 

 numpy转字节

 
import numpy as np
from io import BytesIO
A = np.array([ 
      1, 2, 3, 4, 4,
      2, 3, 4, 5, 3,
      4, 5, 6, 7, 2,
      5, 6, 7, 8, 9,
      6, 7, 8, 9, 0 ]).reshape(5,5)

   
# numpy 转bytes
nda_bytes = BytesIO()
np.save(nda_bytes, A, allow_pickle=False)

# bytes转numpy
nda_bytes = BytesIO(nda_bytes.getvalue())
B = np.load(nda_bytes, allow_pickle=False)
print(np.array_equal(A, B))
 

定义protobuf message

 
ndarray.proto
 
syntax = "proto3";

message NDArray {
    bytes ndarray = 1;
}
 

使用

 
from io import BytesIO
import numpy as np
import ndarray_pb2   #上面ndarray.proto编译成python


def ndarray_to_proto(nda: np.ndarray) -> NDArray:
    """
    numpy转proto
    """
    nda_bytes = BytesIO()
    np.save(nda_bytes, nda, allow_pickle=False)
    return NDArray(ndarray=nda_bytes.getvalue())


def proto_to_ndarray(nda_proto: NDArray) -> np.ndarray:
    nda_bytes = BytesIO(nda_proto.ndarray)
    return np.load(nda_bytes, allow_pickle=False)



A = np.array([ 
      1, 2, 3, 4, 4,
      2, 3, 4, 5, 3,
      4, 5, 6, 7, 2,
      5, 6, 7, 8, 9,
      6, 7, 8, 9, 0 ]).reshape(5,5)
serialized_A = ndarray_to_proto(A)
deserialized_A = proto_to_ndarray(serialized_A)
assert np.array_equal(A, deserialized_A)
 
 
 

Tags: protobuf
评论(365) 阅读(9287)

socketio与apscheduler并用

2019年4月28日 10:16

 

 说明

 
flask项目引入了flask-socketio提供websocket通信,同时需要flask-apscheduler完成定时任务。
 
 

 问题描述

 
项目初期只有socketio,并且以gunicron运行
 
gunicorn --worker-class eventlet -w 1 zhima_chat:app -b 0.0.0.0:5000 --access-logfile -
 
后来要引入apscheduler
 
以上面的方式运行,出现了问题。该如何将socketio与apschedeuler结合呢?
 
 

Tags: flask apscheduler python socketio
评论(114) 阅读(37966)

docker运行gunicorn看不到控制台输出

2019年4月06日 07:59

问题描述

 
项目做成了docker镜像, 以gunicorn命令运行, print输出为什么没有写到docker日志?
 
<!--more-->
 
* docker-compose文件如下
 
version: '2'
services:
  xxx.xxx:
    image: xxx:1.0
    volumes:
        - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
        - ./log/xxx:/var/log/xxx
        - ./xxx:/app
    ports:
      - "5000:5000"
    environment:
      - FLASK_ENV=development_wyq
    command: gunicorn --worker-class eventlet -w 1 xxx:app -b 0.0.0.0:5000      # print不写到控制台
    #command: gunicorn --worker-class eventlet -w 1 xxx:app -b 0.0.0.0:5000 --access-logfile -   #print写到控制台
 
 

解决方法

 
在命令上加上"--access-logfile -"
 
来源 
 
此生必看的科学实验-水知道答案
《了凡四训》详解之改过之法
印光大师十念法(胡小林主讲第1集)
精神病为什么治不好
百病之源
 

Tags: docker flask websocket gunicorn
评论(31) 阅读(6591)

利用qq邮箱作为个人邮件服务器发送邮件

2019年4月06日 07:47

 需求描述
    邮件通知很有用处。如何搭建一个邮件服务器,允许发送邮件呢?
 

实现方法

 
一、利用开源软件,搭建自己的邮件服务器。
二、利用qq等第三方邮件服务。
 
自己搭邮件服务器较麻烦,我的需求比较简单,利用qq邮件,发送邮件即可,下面是方法.
 
 

Tags: 运维
评论(65) 阅读(6759)

用最简单方法解决api接口安全问题,几乎无法破解

2019年1月01日 11:45

 

 场景描述

 
项目需要为第三方提供api服务接口。接口涉及到核心功能,如何保证接口安全。防止伪造身份、篡改数据?
 

思路

 
保障数据安全最好的方法,当然是加密了。无法解析内容,自然无法伪造,篡改。

可是使用https证书需要收费的。有其它方法么?

有的。

消息哈希认证(hmac)。
 
 

 算法描述

 
*  访问者
 
1. 当访问接口时, 将参数按key值排序,组成key1=value1&key2=value2&....&secret_key=...
2. 然后对上面结果做md5,生成签名sign
3. 将sign放到加入请求的参数
 
* 被访问者
 
密钥是被访问者提供了,它也有访问者的secret_key.
1.根据app_id查到secret_key
2.处理请求参数,按规则组成key1=value1&key2=value2....&secret_key=...
3.对上一步结果做md5,生成sign。比较两个sign,相等则身份验证通过
 

效果

 
1. 密钥只存在双方机器上,不可能被截取
2. 签名无法伪造,同样身份无法伪造、消息无法被篡改
 
使用了hmac认证,接口被破解基本是不可能的
 

python实现

 
import md5

app_id=123
secret_key="xxxxxxxx"

request_param = dict(
    key1="value1",
    key2="value2",
    key3="value3"
)

def sign():
    params = ["%s=%s" % (key, value) for key, value in sorted(request_param.items(), key=lambda item: item[0])]
    params.append("secret_key=%s" % secret_key)
    str_param = "&".join(params)
    print str_param
    md = md5.md5()
    md.update(str_param)
    return md.hexdigest()

if __name__ == '__main__':
    print(sign())
 
 
来源
 

 

Tags: web python
评论(55) 阅读(7662)

uwsgi: option is ambiguous: http

2018年11月25日 15:08

 
 
从官网上下载的uwsgi包含所有的插件。其它来源的uwsgi可能被人改过了。例如,为了减小体积,会删除uwsgi不必要的插件。
 

环境

 
alpine中运行uwsgi
 
 

示例:启动uwsgi提供http访问接口,出现错误

 
#报错的原因就是这个uwsgi没有http插件
~ # uwsgi --http :9090  --plugins http,python3
[uWSGI] getting INI configuration from /app/uwsgi.ini
uwsgi: option is ambiguous: http
getopt_long() error
 

看看官网下载的uwsgi

 
 

解决办法

 
#查找
/app # apk search uwsgi |grep http
uwsgi-router_http-2.0.17-r0
uwsgi-http-2.0.17-r0

#安装
apk add uwsgi-http   uwsgi-router_http
 

来源

 
 

Tags: uwsgi docker
评论(1) 阅读(3078)

pyinstaller黑色窗口一闪而过怎么去掉

2018年10月14日 08:32

默认情况下pyinstaller编译出的exe有cmd窗口。给它设置了-w选项,可以把它去掉。
为什么我加了,还是会出现一闪而过的黑色窗口
 

加上-w选项

 
python -m PyInstaller xxx.py -w
 

不要用os.system

 
os.system("xxx")  #会调用cmd.exe,所以会出现黑色窗口

#改用subprocess,不会出现黑色窗口
subprocess.Popen("xxx", close_fds=True)
 
《了凡四训》详解之改过之法
印光大师十念法(胡小林主讲第1集)
《寿康宝鉴》有声书

 

评论(12) 阅读(3417)

pyinstaller编译出的exe被杀毒软件认为是木马

2018年10月14日 08:15

把python文件转为独立的exe,放在windows上运行。结果腾讯管家马上提示有可能是木马把它删除了
 
一脸懵逼,我良民,咋成木马了呢。思前想后,我做了什么,被认为很危险。原来我用了os.system。下面是演示
 

python文件

#xxx.py
import os
cmd="xxxxx"
os.system(cmd)
 

编译exe

#编译完成,系统提示发现木马
python -m PyInstaller xxx.py --onefile
 

解决方法

 
改用subprocess替代os.system
 
subprocess.Popen(cmd, close_fds=True)
 

为什么用了subprocess就没事了?

 
os.sytem属于独立进程间调用。subprocess外壳还是python属于内部进程。
 
 
《了凡四训》详解之改过之法
印光大师十念法(胡小林主讲第1集)
印光大师十念法(胡小林主讲第2集)
印光大师十念法(胡小林主讲第3集)
 
 

Tags: pyinstaller
评论(9) 阅读(5910)

pelican AttributeError: 'unicode' object has no attribute 'slug'

2018年7月08日 16:06

pelican生成html报错,原因是网上的教程有错误。pelican默认支持rst格式,创建md格式文章,根本没有编译,所以网上教程不会报错
 
如果支持markdown需要安装”pip install markdown”。安装之后编译出现如下错误。
 
(pelican) ➜  xuefo make html
pelican /home/wyq/me/workspace/xuefo/content -o /home/wyq/me/workspace/xuefo/output -s /home/wyq/me/workspace/xuefo/pelicanconf.py 
CRITICAL: AttributeError: 'unicode' object has no attribute 'slug'
make: *** [Makefile:65:html] 错误 1
 
错误方式
Title: 文章标题
Date: 2018-07-07
Category: 文章类别
Tag: 标签1, 标签2       #此处错误
 
正确方式
Tags: 标签1,标签2
 
《了凡四训》详解之改过之法
印光大师十念法(胡小林主讲第1集)

 

评论(421) 阅读(9943)