Python Forum
Trying to separate a loop across multiple threads
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to separate a loop across multiple threads
#1
Hey guys, what I am trying to perform is a reqMktData pull (I'm pulling the bid,ask,last prices) for multiple tickers. Normally I would just change the ID number for each ticker.. but for this specific program I am building. The ticker amounts are going to change each day. One day it could be 3 stocks, next day it could be 7.

So I am trying to come up with a fancy way to "spin off" the reqMktData into its own threads so that can change by the input.

What is happening is, that the threads spin off fine, but the reqMktData just keeps getting "replaced" by the next ticker in line in the function.

Really what I need it to do is this

1) For ticker in tickers (the loop through). After we loop the first one. Stop the function and loop. And seal that up as the first thread.
2) Run the function again, for ticker in tickers (the loop) but we pick back up where we left off. Creates another ReqMktData in the 2nd thread. Stop the function and loop from continuing.
3) Function runs again, for ticker in tickers (the loop), and we once again pick back up where we left off. Creates another reqMktData in the 3rd thread. Stop the function and loop.

Since there is only 3 "tickercount" its all finished. Now I have 3 threads all running a reqMktData but each thread has a unique "tickerId" corresponding with each stock in the tickers list.

Does that make sense? Am I just over complicating this? Here is the code for you to study. Its obviously not the whole program. Everything else in the program works fine.

tickerId = 0
tickercount = 0

tickers = ["JG", "AAPL", "FB"]

## Signals & Variables ##

def dataReturn(app):
    global tickerId
    
    for ticker in tickers:
        
        tickerId += 1
        app.reqMktData(tickerId, usTechStk(ticker), "", False, False, [])
        time.sleep(5) #Add some breathing room for testing
        

threads = []

for ticker in tickers:
    tickercount += 1
        
for _ in range(tickercount):
    t = threading.Thread(target=dataReturn, args=(app,))
    t.start()
    threads.append(t)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,576 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Multiple Loop Statements in a Variable Dexty 1 1,176 May-23-2022, 08:53 AM
Last Post: bowlofred
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,331 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Multiple user defined plots with secondary axes using for loop maltp 1 1,393 Apr-30-2022, 10:19 AM
Last Post: maltp
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,845 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Multiple MEAN value using FOR loop kka_anand 2 2,121 Jul-14-2020, 06:20 PM
Last Post: kka_anand
  Get average of multiple threads Contra_Boy 1 14,041 May-05-2020, 04:51 PM
Last Post: deanhystad
  How to Display Multiple Time Tables With While Loop ZQ12 2 2,118 Nov-10-2019, 04:15 AM
Last Post: ZQ12
  pytest loop through multiple tests? burvil 0 4,283 Sep-26-2019, 11:42 PM
Last Post: burvil
  Pasting multiple images on to one image with a for loop. DreamingInsanity 2 6,416 Feb-01-2019, 12:39 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020