I'm relatively new to Python and last touched threads and processes in C about 7 years ago, so please treat me as a newb in your responses.
I'm using Python 2.7.6 on Linux.
I am trying to query (and later download from) an online archive which only allows one connection per registered user and is pretty slow. It has its own API for the queries so I won't go in to that. I'm intending to perform the queries and later the downloads in parallel threads, one per user account. (for the record I'm not cheating the system, all the accounts are genuine users!)
accounts = ['user1','pass1','user2','pass2'...]
queries = ['query1','query2','query3',..., 'queryN' ]
numQueries = len(queries)
numAc = len(accounts)/2
if numQueries < numAc:
nThreads = numQueries
else
nThreads = numAc # most likely situation
# example of function for the query
def runQuery(user, passw, query):
# here's the API bit
Every example I have seen runs over one single list.
So, I'm at a loss. I can see how it would work if we forget all about accounts and constraints and were just running different queries.
How can I set up one thread per account and iterate over the list of queries/downloads? Remembering I'm using 2.7.
I'm also getting overwhelmed by the thread/process issue, so would appreciate lots of clarity in responses.
--- Edit - As code in comment below is unreadable, this is what I tried:
ulock = thread.allocate_lock()
def runQuery(userQueue, ulock, queryQueue):
query = queryQueue.get()
with ulock:
user = userQueue.popleft()
userQueue.append(user)
passw = userQueue.popleft()
userQueue.append(passw)
print 'The executed query will use: ' + user + ' ' + passw + ' ' + ' ' + query + '\n'
for t in nThreads:
thread.start_new_thread(runQuery, (userQueue, ulock, queryQueue,))