使用来自 MySQL 查询的 PyQt 缓慢渲染 PDF [英] Slow PDF render with PyQt from MySQL query

查看:38
本文介绍了使用来自 MySQL 查询的 PyQt 缓慢渲染 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到昨天,我还在为我的应用程序使用 SQLite.今天我找到了一个足够强大的理由(应用程序的多个实例)使用 pymysql 切换到 MySQL.

Until yesterday, I was using SQLite for my application. Today I found a reason strong enough (multiple instances of application) to switch to MySQL using pymysql.

有一次,我的应用程序在数据库中查询了 300 行:

At one point, my application queries the database for 300 rows:

cur.execute('select ime,brPredmeta,statusStr,sudskiBr ,sudija ,datumRasprave,statusPredmeta, zaduzen,datumZaduzenja from predmeti')
sviaktivni = cur.fetchall()
sviaktivni = list(sviaktivni) #make a list of tuples
sviaktivni.sort(key=operator.itemgetter(0)) #sort the list

之后,我提取变量,然后使用这些变量制作 HTML 文档,然后将其发送到打印机(PDF 或常规打印机).生成 HTML 的代码非常简单;带有几行标题"的表格(为了清楚起见,此处省略),然后是另一个正文"表格,代码如下:

After that, I extract the variables and then use those variables to make a HTML document, which is then sent to printer (PDF or regular printer). Code for generating HTML is pretty simple; table with few rows for "header" (omitted here for clarity), then another table for "body" with code like this:

#extract the variables
for tuple in sviaktivni:
    ime,brPredmeta,statusStr,sudskiBr ,sudija ,datumRasprave,statusPredmeta, zaduzen,datumZaduzenja = tuple
#done extracting

  body = ('<html><head><title></title>'
    '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'
    '<style></style>'
    '</head>'
    '<body>'
    '<table align ="center" border="0" width="100%" style="table-layout:fixed">'
    '<tr height="10%">'
    '<td align="left" width="20">'+str(brojac)+'.</td>'
    '<td align="left" width="180">'+ime+'</td>'
    '<td align="left" width="100">'+str(brPredmeta)+'</td>'
    '<td align="left" width="80">'+statusStr+'</td>'
    '<td align="left" width="80">'+str(sudskiBr)+'</td>'
    '<td align="left" width="140">'+sudija+'</td>'
    '<td align="left" width="150">'+zaduzen+'</td>'
    '<td align="left">'+datumZaduzenja+'</td>'
    '<td align="right"> '+statusPredmeta+' </td>'
    '</tr>'
    '</table>'
    '</body>'
    '</html>')

  self.ui.printHTML.append(body)

我的问题是查询是立即执行的,但之后 CPU 卡在 100%,整个程序就死机了.打印预览对话框有时会显示数据,有时它只是一个空窗口.无论哪种方式,整个应用都会被冻结.

My problem is that the query is executed instantly, but after that CPU is stuck at 100%, and the whole program just freezes. Print preview dialog is sometimes shown with data, sometimes it's just an empty window. Either way, whole app is frozen.

使用 SQLite 的相同代码没有问题.我应该怎么办?回到 sqlite 并冒数据库损坏的风险?

There were no problems with the same code using SQLite. What should I do? Get back to sqlite and risk database corruption?

推荐答案

不要只是将整个 SQL 结果加载到内存中,并期望这适用于所有情况.而且,用数据库给你做排序,这就是它的优点.

Don't just load the whole SQL result into memory, and expect this to work for all cases. Moreover, use the database to do the sorting for you, that's what it is good at.

只需遍历游标并一个一个处理结果,这样您就不必用中间结果填满内存:

Simply loop over the cursor and process results one by one, that way you don't have to fill up memory with intermediate results:

cur.execute('''
    select 
        ime, brPredmeta, statusStr, sudskiBr, sudija,
        datumRasprave,statusPredmeta, zaduzen,datumZaduzenja
    from predmeti
    order by ime
    ''')
for row in cur:
    ime, brPredmeta, statusStr, sudskiBr, sudija, datumRasprave, statusPredmeta, zaduzen, datumZaduzenja = row
    # Process row data.

这篇关于使用来自 MySQL 查询的 PyQt 缓慢渲染 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆