在HTTPServer中使用SQLAlchemy会话。线程错误

时间:2018-10-04 20:32:41

标签: python python-3.x sqlalchemy

我在使用SQLAlchemy会话在另一个线程上生成HTTP Server时遇到问题(我在另一个线程上生成HTTP Server的原因是,否则测试会永远挂起)。发生的事情是我在每个请求中传递了数据库配置。但这不允许我在使用SQLite的测试服务器(开发服务器为PostgreSQL)上创建会话。每次进入视图时,都会引发以下错误:

library ieee;
use ieee.std_logic_1164.all;

entity DEL_INV is
  generic(
    D: time;
    XOUT: std_logic);
  port(
    INPUT: in std_logic;
    OUTPUT: out std_logic);
end entity DEL_INV;

architecture s of DEL_INV is
  signal PRE : std_logic;
begin
  PRE <= (not INPUT) after D;
  OUTPUT <= XOUT when is_x(PRE) else PRE;  -- Drive XOUT if is_x to clean up
end architecture s;


library ieee;
use ieee.std_logic_1164.all;

entity OSCILLATOR is
  port(
    OUTPUT: out std_logic);
end entity OSCILLATOR;

architecture structural of OSCILLATOR is

component DEL_INV is
  generic(
    D: time;
    XOUT: std_logic);
  port(
    INPUT: in std_logic;
    OUTPUT: out std_logic);
end component DEL_INV;

for all: DEL_INV use entity work.DEL_INV(s);

signal conn  : std_logic;
signal conn1 : std_logic;
signal conn2 : std_logic;

constant DE : time := 2 ns;

begin

    INV1: DEL_INV generic map(de, '0') port map (conn, conn1);
    INV2: DEL_INV generic map(de, '1') port map (conn1, conn2);
    INV3: DEL_INV generic map(de, '0') port map (conn2, conn);

    OUTPUT <= conn;

end architecture;

这是我的测试用例:

sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 5784 and this is thread id 8560

我的应用程序安装程序类:

class ViewsTestSuite(unittest.TestCase):


    def setUp(self):
        app = App(8000, testing=True)
        try:
            httpd_thread = threading.Thread(target=app.run)
            httpd_thread.daemon = True
            httpd_thread.start()
        except KeyboardInterrupt:
            pass

和我正在测试的视图:

class App:

    def __init__(self, port, testing=False):
        self.port = port
        self.testing = testing
        self.server = None
        self.config = None
        self.db = None

    def run(self):
        if self.testing:
            self.config = TestingConfig
        else:
            self.config = DevelopmentConfig
        self.init_db()
        RequestHandler.config = self.config
        self.server = HTTPServer(('localhost', self.port), RequestHandler)
        try:
            self.server.serve_forever()
        except KeyboardInterrupt:
            pass
        self.server.server_close()

    def stop(self):
        self.server.server_close()

    def init_db(self):
        self.db = Database(self.config.DATABASE_URL)
        self.db.create_session()
        self.db.create_database()

任何帮助都将非常惊人!非常感谢

0 个答案:

没有答案