首页 \ 问答 \ SQLAlchemy Core插入使得(金字塔)范围的会话挂起(SQLAlchemy Core inserts make (Pyramid) scoped session hanging)

SQLAlchemy Core插入使得(金字塔)范围的会话挂起(SQLAlchemy Core inserts make (Pyramid) scoped session hanging)

我有很多子对象要添加到父SQA对象,有时10,000左右。 代码在Pyramid应用程序中运行,该应用程序使用作用域会话和ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

这没有问题:

lines = [{'col1':1, 'col2':2, 'related_id':1}, ...]
for d in lines:
    d['related_id'] = related_id
    f = SQAObject(**d)
    DBSession.add(f)

为了性能,我使用SQLAlchemy Core insert重写了这个:

lines = [{'col1':1, 'col2':2, 'related_id':1}, ...]
rbrf_table = SQAObject.__table__
eng = DBSession.get_bind()
conn = eng.connect()
for d in lines:
    d['related_id'] = related_id
conn.execute(rbrf_table.insert().values(lines))
conn.close()
eng.dispose()

这会导致一个相当奇怪的问题:代码执行(更快),但HTTP请求完成后,结果不会返回给HTTP客户端。 相反,Python进程会无限期地持续占用100%的CPU(我等了半个小时,以确定它是否在任何时间完成)。

我检查了Postgres锁,以确保它们不会阻止它以及活动( select * from pg_stat_activity ),但似乎没有任何异常。

可能是这个问题的原因是什么?

PS怀疑配置一个引擎可能会导致这个问题,我运行相同的代码有和没有eng.dispose() ,具有相同的结果。

UPDATE

情节变浓了。 如果我执行单个插入,它可以工作:

rbrf_table = RTABomRowFile.__table__
eng = DBSession.get_bind()
conn = eng.connect()
for d in lines:
    ...
    ins = rbrf_table.insert().values(d)
    conn.execute(ins)
conn.execute('COMMIT')
conn.close()
eng.dispose()

I have lots of child objects to add to a parent SQA object, sometimes 10,000 or so. The code runs in Pyramid app, which makes use of scoped session and ZopeTransactionExtension:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

This works without a problem:

lines = [{'col1':1, 'col2':2, 'related_id':1}, ...]
for d in lines:
    d['related_id'] = related_id
    f = SQAObject(**d)
    DBSession.add(f)

For performance I rewrote this using SQLAlchemy Core inserts:

lines = [{'col1':1, 'col2':2, 'related_id':1}, ...]
rbrf_table = SQAObject.__table__
eng = DBSession.get_bind()
conn = eng.connect()
for d in lines:
    d['related_id'] = related_id
conn.execute(rbrf_table.insert().values(lines))
conn.close()
eng.dispose()

This causes a rather weird problem: the code executes (faster), but after HTTP request is finished, the result is not returned to HTTP client. Instead the Python process keeps consuming 100% CPU indefinitely (I waited half an hour to make sure if it completes in any time).

I checked Postgres locks to make sure they do not block it as well as activity (select * from pg_stat_activity), but there does not seem to be anything unusual.

What might be the cause of this problem?

P.S. Suspecting that disposing an engine might cause this problem, I've ran the same code with and without eng.dispose(), with the same result.

UPDATE

The plot thickens. If I execute individual inserts instead, it works:

rbrf_table = RTABomRowFile.__table__
eng = DBSession.get_bind()
conn = eng.connect()
for d in lines:
    ...
    ins = rbrf_table.insert().values(d)
    conn.execute(ins)
conn.execute('COMMIT')
conn.close()
eng.dispose()

原文:https://stackoverflow.com/questions/33692443
更新时间:2023-04-03 16:04

最满意答案

使用U2U而不是另一个软件,我能够通过删除和移动AND来解决我的问题。


Using U2U instead of another piece of software I was able to solve my problem by removing and moving the AND.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)