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

  • SEARCH
  • Home
  • 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 7524315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:04:04+00:00 2026-05-30T03:04:04+00:00

I can’t figure out what I’m doing wrong(or how to correct it). It might

  • 0

I can’t figure out what I’m doing wrong(or how to correct it). It might be easier to show some code(its a bit simplified from what I’m doing but it proves my point):

from multiprocessing import Pool
import MySQLdb
import sys

#sql connection
try:
    conn = MySQLdb.connect (host = "127.0.0.1",user = "user",passwd = "password", db = "mydb")
except MySQLdb.Error, e:
     print "Error %d: %s" % (e.args[0], e.args[1])
     sys.exit (1)

#with database
cursor = conn.cursor ()
cursor.execute ("DROP TABLE IF EXISTS data_table")
cursor.execute ("""
    CREATE TABLE data_table(
        value     CHAR(80)
    ) ENGINE=MyISAM 
""")

cursor.execute (""" INSERT INTO data_table (value) VALUES ('%s')""" % [0, 0]) #need to insert basecase
conn.commit()


def build_table(i,x):    # i is index, x is data[i]
    conn = MySQLdb.connect (host = "127.0.0.1",user = "user",passwd = "password", db = "mydb")
    cursor = conn.cursor ()
    #print i,x
    target_sum = 100
    for s in range(target_sum + 1): 
        for c in range(target_sum + 1): 
            #print c, i
            cursor.execute ("""
INSERT INTO data_table (value) 
   SELECT  '%s'
   FROM dual
   WHERE ( SELECT COUNT(*) FROM data_table WHERE value='%s' )
         = 1
     AND NOT EXISTS
         ( SELECT * FROM data_table WHERE value='%s' )
           """ % ([s, i+1], [s - c * x, i], [s, i+1]))
            conn.commit()

    conn.close()

data = [2,5,8]
pool = Pool(processes=4)
for i, x in enumerate(data): 
    build_table(i,x) #creates 250 records
    #pool.apply_async(build_table, (i, x))
pool.close()
pool.join()

print 'completed'

It basically creates a table in mysql. The code above creates 250 entries(which is correct), but if you comment out build_table(i,x) in the for loop and uncomment pool.apply_async(build_table, (i, x)) it creates only 52 records. Why is there a difference when multiprocessing the same function and is there anything I can do to fix it so the results are the same(I thought quickly committing updates would fix it but no luck)?

If I play around pool = Pool(processes=4) and change it to 1, it works but I guess thats expected because its not multiprocessing really at that point. Also, if it helps I’m using InnoDB.

UPDATE: when I change to MyISAM I get 240 results being updated(not quite the 250 I need but much better than 52).

UPDATE2: mysql command was combined into a single command, and results seem to vary. Sometimes I get 248 results in the database, sometimes 240 or less. Maybe multiprocessing is causing this diverge between expected and actual results ?

  • 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-30T03:04:05+00:00Added an answer on May 30, 2026 at 3:04 am

    I would try to combine the 2 Selects and the Insert in one Insert statement:

    #print c, i
    cursor.execute(""" SELECT value FROM data_table WHERE value='%s' """ % ([s - c * x, i]))
    if cursor.rowcount == 1:
        cursor.execute(""" SELECT value FROM data_table WHERE value='%s' """ % [s, i+1])
        if cursor.rowcount == 0:
            cursor.execute (""" INSERT INTO data_table (value) VALUES ('%s')""" % [s, i+1])
    

    Into something like:

    #print c, i
    cursor.execute ("""
        INSERT INTO data_table (value) 
           SELECT  '%s'
           FROM dual
           WHERE ( SELECT COUNT(*) FROM data_table WHERE value='%s' )
                 = 1
             AND NOT EXISTS
                 ( SELECT * FROM data_table WHERE value='%s' )
                   """ % ([s, i+1], [s - c * x, i], [s, i+1]))
    

    Not sure about the syntax in the last line. You’ll need to pass 3 parameters.

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

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can you suggest some good MVC framework for perl -- one I am aware
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anybody shed some light on getDay() in Javascript please. Here datepicker is textbox
Can anybody tell me a regular expression to use within some PHP to find
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
Can i get the source code for a WAMP stack installer somewhere? Any help
Can some one Guide me to work with these things... What is Model popup

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.