Firstly, the SA docs on Session.add() do not mention anything about the method's return value, so I would assume it is expected to return None
.
Secondly, I think you meant to add new_movie
to the session, not movies(**movie_details)
, whatever that is :)
Thirdly, the standard Pyramid session (the one configured with ZopeTransactionExtension) is tied to Pyramid's request-response cycle, which may produce unexpected behavior in your situation. You need to configure a separate session which you will need to commit manually in store_movie_details
. This session needs to use scoped_session so the session object is thread-local and is not shared across threads.
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
session_factory = sessionmaker(bind=some_engine)
AsyncSession = scoped_session(session_factory)
def store_movie_details(movie_id):
session = AsyncSession()
movie_details = scrape_data(movie_id)
new_movie = Movie(**movie_details) # Movie is my model.
session.add(new_movie)
session.commit()
And, of course, this approach is only suitable for very light-weight tasks, and if you don't mind occasionally losing a task (when the webserver restarts, for example). For anything more serious have a look at Celery etc. as Antoine Leclair suggests.
2014年2月27日 ... 多线程最好不要共用一个连接以下是来自MSDN的一段话http://msdn.microsoft.com/ zh-cn/library/ms131686.aspx“SQL Server 2005 在访问数据库 ...
Firstly, the SA docs on Session.add() do not mention anything about the method's return value, so I would assume it is expected to return None .
2018年2月22日 ... 运行程序,如果数据库已经存在,则删除重建。当打开连接以及单独使用 OpenAsync和ExecuteNonQueryAsync方法执行SQL命令时,我们使用 ...
2016年12月14日 ... 数据库操作是异步的,在高并发的情况下有可能出现以下的情形: 1、线程A对 数据库的表table_1进行插入操作,同时另一线程B同样对表table_2 ...
Jun 25, 2006 ... The hardware trend toward multithreading for improved performance, including multicore and multithreaded chip designs [1, 11, 13, 17, 21, 23, 24] ...
2019年4月20日 ... 我这边有个项目,是使用客户端socekt 连接服务获取数据并保存到数据库,但是 由于是一个客户端连接多个服务,目前只想到了多线程,但是在 ...
2021年1月27日 ... 使用多线程操作mysql数据库时,如果使用普通的连接,会出现数据重复的问题, 应该使用数据库连接池. 解决方法:使用数据库连接池,并且每次 ...
这个要么业务层加锁,要么使用版本号,加一个字段,或者使用数据库事物功能。 数据库本身的行级锁没用。行级锁只能保证更新该行的时候不会有其他线程操作 ...
You can use pcntl_fork inside your script to duplicate the thread and process 1000 entries per child thread.
A database instance contains or interacts with multiple processes. ... In the multithreaded architecture, an Oracle process can be an operating system process or ...
今天做多线程操作数据库,发现好多问题呢,现在整理如下: 1,com.microsoft. sqlserver.jdbc.SQLServerException: 没有需要传输的数据。
2019年9月26日 ... 网上搜罗了一大堆所谓的多线程操作数据库,保证事务同时提交回滚啊,有的文章 有点含量,有的就是纯属胡诌,浪费同学们的感情和时间,不过 ...
2020年5月28日 ... 多线程. 单进程和多进程结果一样。 同时进行数据库的读操作不会产生任何问题;; 如果都需要创建表 ...
May 24, 2011 ... Here is my opinion: Usually the bottleneck (or slowest part) of a DB system is the disk. The CPU only spikes during arithmetic operations, ...
Apr 25, 2017 ... Learn why Spring transactions over multiple threads fail, and how to use ... framework provides a comprehensive API for database transaction ...
When you use databases in multithreaded programs, be aware of these ... Database operations that are threadsafe include: Create File, Add Member, Delete ...
Nov 27, 2017 ... Database operations typically consist of multiple stages and allow for quite some parallel execution. Your single thread will block that.
Feb 10, 2019 ... If you want to run this operation for more than one customer, you need to sequentially run the procedure for each of the selected customers. In a ...
In computer architecture, multithreading is the ability of a central processing unit ( CPU) to provide multiple threads of execution concurrently, supported by the operating system ... The purpose of interleaved multithreading is to remove all data dependency stalls from the execution pipeline. Since one thread is relatively ...