Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dev][bug]运行一定时间后,recoder抛异常 #26

Closed
yinianfangxia opened this issue Sep 7, 2019 · 2 comments
Closed

[dev][bug]运行一定时间后,recoder抛异常 #26

yinianfangxia opened this issue Sep 7, 2019 · 2 comments

Comments

@yinianfangxia
Copy link
Contributor

异常如下:

sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 139991270131456 
and this is thread id 139991166502656. [SQL: 'SELECT "index".id AS index_id, "index".entity_id AS index_entity_id, "index".timestamp AS index_timestamp, "index".entity_type AS index_ent
ity_type, "index".exchange AS index_exchange, "index".code AS index_code, "index".name AS index_name, "index".is_delisted AS index_is_delisted, "index".category AS index_category, "inde
x".base_point AS index_base_point, "index".list_date AS index_list_date \nFROM "index" \nWHERE "index".exchange IN (?) ORDER BY "index".code ASC'] [parameters: [{}]] (Background on this
 error at: http://sqlalche.me/e/f405)
ERROR  ThreadPoolExecutor-0_0  2019-09-08 07:37:41,624  __main__:25  run  sina runner error:(sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same t
hread. The object was created in thread id 139991270131456 and this is thread id 139991166502656. [SQL: 'SELECT "index".id AS index_id, "index".entity_id AS index_entity_id, "index".tim
estamp AS index_timestamp, "index".entity_type AS index_entity_type, "index".exchange AS index_exchange, "index".code AS index_code, "index".name AS index_name, "index".is_delisted AS i
ndex_is_delisted, "index".category AS index_category, "index".base_point AS index_base_point, "index".list_date AS index_list_date \nFROM "index" \nWHERE "index".exchange IN (?) ORDER B
Y "index".code ASC'] [parameters: [{}]] (Background on this error at: http://sqlalche.me/e/f405)
Traceback (most recent call last):
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1127, in _execute_context
    context = constructor(dialect, self, conn, *args)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 639, in _init_compiled
    self.cursor = self.create_cursor()
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 954, in create_cursor
    return self._dbapi_connection.cursor()
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/pool.py", line 977, in cursor
    return self.connection.cursor(*args, **kwargs)
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 139991270131456 and this is thread id 139991166502656.

The above exception was the direct cause of the following exception:
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "zvt/recorders/sina/sina_runner.py", line 20, in run
    SinaChinaStockCategoryRecorder().run()
  File "/home/xuanqi/workspace/github/zvtvz/zvt/zvt/recorders/sina/meta/sina_china_stock_category_recorder.py", line 34, in __init__
    return_type='domain', provider=self.provider)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/zvdata/api.py", line 215, in get_entities
    index=index, index_is_time=index_is_time)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/zvdata/api.py", line 115, in get_data
    return query.all()
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2843, in all
    return list(self)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2995, in __iter__
    return self._execute_and_instances(context)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3018, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1132, in _execute_context
    None, None)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 248, in reraise
    raise value.with_traceback(tb)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1127, in _execute_context
    context = constructor(dialect, self, conn, *args)
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 639, in _init_compiled
    self.cursor = self.create_cursor()
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 954, in create_cursor
    return self._dbapi_connection.cursor()
  File "/home/xuanqi/workspace/github/zvtvz/zvt/ve/lib/python3.6/site-packages/sqlalchemy/pool.py", line 977, in cursor
    return self.connection.cursor(*args, **kwargs)
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 139991270131456 and this is thread id 139991166502656. [SQL: 'SELECT "index".id AS index_id, "index".entity_id AS index_entity_id, "index".timestamp AS index_timestamp, "index".entity_type AS index_entity_type, "index".exchange AS index_exchange, "index".code AS index_code, "index".name AS index_name, "index".is_delisted AS index_is_delisted, "index".category AS index_category, "index".base_point AS index_base_point, "index".list_date AS index_list_date \nFROM "index" \nWHERE "index".exchange IN (?) ORDER BY "index".code ASC'] [parameters: [{}]] (Background on this error at: http://sqlalche.me/e/f405)

初步怀疑是跟全局session的修改有关

@yinianfangxia yinianfangxia changed the title [dev]运行一定时间后,recoder抛异常 [dev][bug]运行一定时间后,recoder抛异常 Sep 7, 2019
@yinianfangxia
Copy link
Contributor Author

原因是:目前使用全局session,创建是在主线程,但apscheduler调度的recorder在另外的线程。
解决方案:
1)不使用全局session
2)创建engine时,

    db_path = os.path.join(context['data_path'], '{}_{}.db?check_same_thread=False'.format(provider, db_name))

不限制在单线程使用

目前看两种方法都可以,
1)不使用全局session会更清晰一些。
2)目前写操作都在在单个recorder里面,不会冲突

@foolcage
Copy link
Member

foolcage commented Sep 9, 2019

最新版本已修复。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants