0

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)  
    
eng3
  • 431
  • 3
  • 18
  • How do you load the client? Is it a python module that you import? And how do you reload it later? – tdelaney Nov 24 '14 at 19:40
  • after some investigation, it seems that my problem was different so I've edited the above – eng3 Nov 25 '14 at 13:39

1 Answers1

0

Queue is probably what you're looking for, maybe the link in this previous answer might help you: Sharing data between threads in Python

Community
  • 1
  • 1
eyalzek
  • 255
  • 2
  • 9