python,openbabel,生成图片,svg,png , threading多线程

import pybel
import openbabel
import os
import threading

#只引入了p()函数
from common.functions import \*
#封装了mysql的数据库
from db.MySQL import MySQL
#封装了mongodb的数据库
# from db.Mongo import Mongo

def smilesToIMG(smiles,filename):
    filename1 = filename+".png"
    filename2 = filename+".svg"
    smiles = smiles.replace('\\n','')
    try:
        mol = pybel.readstring("smi", smiles)
    except Exception as e:
        return 'error'
    try:
        mol.draw(show=False, filename=filename1, update=True)
        mol.make3D()
    except Exception as e:
        return 'error'
    mol = pybel.readstring("smi", smiles)
    mol.write("svg", filename2, overwrite=True)
    return 'ok'

def work(L1):
    for v in L1:
        ident = threading.currentThread().ident
        p(ident)
        status = smilesToIMG(v\['smiles'\],v\['filename'\])
        p(status)

def main():
    MySQLConn = MySQL(host='xxx', port=3306, user='root', passwd='xxx', db='xxx')
    datas = MySQLConn.fetchall("select id,smiles from xxx where smiles != '' and structure\_img = ''")
    D2 = \[\]
    p(datas)
    # quit()
    j=0
    for data in datas:
        path = "./upload/" + str(data\['id'\] % 1000)
        if os.path.exists(path):
            p('pass1')
            pass
        else:
            os.makedirs(path)
        filename1 = path+'/' + str(data\['id'\]) + '.png'
        filename = path+'/' + str(data\['id'\])
        if os.path.exists(filename1):
            p('num:'+str(j))
            j+=1
            pass
        else:
            p('file1')
            if data\['smiles'\]:
                D1={'filename':filename,'smiles':data\['smiles'\]}
                D2.append(D1)

    xc = 20
    one = len(D2)//xc
    for i in range(xc + 1):
        L1 = D2\[i \* one:(i + 1) \* one\]
        t = threading.Thread(target=work, args=(L1,))
        t.start()


if \_\_name\_\_ == '\_\_main\_\_':
    main()
    print('ok')

王加文博客
请先登录后发表评论
  • latest comments
  • 总共0条评论