简体   繁体   中英

Python threading or multiprocess questions with sqlite3 and matplotlib

I have a python script that I'd like to run using two processes or threads. I am limited to two because I am connecting to an api/link which only has two license. I grab the license by importing their module and instantiating their class. Here are my issues:

  1. I need to write to a sqlitedb3. I tried to share a db connection, pass it to the "worker" and have it create its own cursor but I will get stuck with a "database locked" message and it seems no matter how long I keep retrying, the lock doesnt clear. My program will spend about 5min loading data from a model, then about a minute processing data and inserting into the db. Then at the end before I move to the next model, it does a commit(). I think I can live with just creating two separate databases though

  2. After it writes to the database, I use matplotlib to create some plots and images then save them to a file with a unique name. I kept getting "QApplication was not created in the main() thread" and "Xlib: unexpected async reply". I figure that switching from threading to multiprocess may help this

  3. I want to make sure only two threads or processes are running at once. What is the best way to accomplish this. With threading, I was doing the following:

     c1 = load_lib_get_license() c2 = load_lib_get_license() prc_list = list of models to process while (len(prc_list) > 0): if not t1.is_alive(): t1 = threading.Process(target=worker,args=(c1,db_connection,prc_list.pop(0)) t1.start() if not t2.is_alive(): t2 = threading.Process(target=worker,args=(c2,db_connection,prc_list.pop(0)) t2.start() while (t1.is_alive() and t2.is_alive(): sleep(1) 

队列可能是您想要的,也许上一个答案中的链接可能对您有所帮助: 在Python中的线程之间共享数据

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM