Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3850998
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:05:43+00:00 2026-05-19T17:05:43+00:00

I am trying to write a threaded Python script which will iterate through a

  • 0

I am trying to write a threaded Python script which will iterate through a list of urls and open each one in a separate thread.

from BeautifulSoup import BeautifulSoup
from threading import Thread
import mechanize

tickers = ["aapl", "siri", "goog", "intc"]
nextTicker = 0

def quotes(i):
    br = mechanize.Browser()
    br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT   6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10')]
    r= br.open('http://finance.yahoo.com/q?s=' + tickers[nextTicker])
    html = r.read()
    soup = BeautifulSoup(html)
    price = soup.findAll('span', attrs={"id":"yfs_l10_" + tickers[nextTicker]})
    price = price[0].string
    print price

for i in range(4):
    t = Thread(target=quotes, args=(i,))
    t.start()

I know that I need a nextTicker = nextTicker + 1 in there so that each thread will grab a unique ticker symbol from the list named tickers but I am not sure where to put this or how to ensure that each thread is getting a unique url.

Right now when the script runs it just grabs the index 0 item from the list for all four threads. How do I get each thread to grab the next item in the list and append it to my base url?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:05:44+00:00Added an answer on May 19, 2026 at 5:05 pm

    If you want thread specific data, pass it in the arguments.

    So use tickers[i] instead of tickers[nextTicker]

    Better yet, use

    for ticker in tickers:
       t = Thread(target = quotes, args = (ticker,) )
    

    Possibly better yet, checkout eventlet. It allows writing code like this but avoids some of the problems with threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an editor overtop a multi-threaded game engine. In theory, through
I'm trying write a query to find records which don't have a matching record
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a blog post which includes a code segment inside a
I am trying to write a unit test for an action method which calls
I'm trying to write a fairly simple threaded application, but am new to boost's
Im trying to write something which very quickly calculates random numbers and can be
Im trying to write a query that will tell me how much time a
I'm trying to write a multi-threaded program, the number of threads based on command-line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.