Hi Ray,
No ouch from me I hope. I thought your reply was very excellent and explained a lot about threading. I also think your advice to not use threading is sound for this current case of getting quotes.
I think I also might have failed to understand the original problem, so some clarification from Patrick might help.
Hi Patrick,
When I look at the code you posted (and if I'm seeing correctly), I now see something that seems problematic. Near the bottom, you have a loop that is spawning 3 threads (i.e., apparently one for each ticker symbol based on the count of tickers).?
Each thread, however, is targeted to dataReturn which is making 3 reqMktData requests, one for each of the tickers. So, the end result is 9 reqMktData requests (i.e., 3 from each thread).
But, (and here's where it gets fun), since tickerId is global, and each thread is bumping it, and there are no locks around the bump, you have a classic race condition that will likely lead to chaos.
So, you could be seeing a case where one thread tries to bump the tickerId for the next reqMktData request with the next symbol, but another thread was in the process of bumping it for a different symbol, and the end result could be that the same tickerId value
is assigned to two different symbols. Processing from this point is unpredictable.
I agree with Ray that threads are probably not needed for your case, but just for fun, here's your code with some adjustments that might get this to work:
----------------------------------------------------------------------
tickers = ["JG", "AAPL", "FB"]
def dataReturn(app, tickerId):
??? app.reqMktData(tickerId, usTechStk(tickers[tickerId]), "", False, False, [])
? ? time.sleep(5) #Add some breathing room for testing
threads = []
for tickerId in range(1, len(tickers)+1):
? ? t = threading.Thread(target=dataReturn, args=(app, tickerId))
? ? t.start()
? ? threads.append(t)
-----------------------------------------------------------------------------
Cheers,
Scott
? ? ? ?