I've just bought a reasonably capable quad core desktop and I want to start utilizing some of the advantages that multi core processing can offer.

I have two simple counting functions, and I would like them to execute simultaneously.

Does anyone know where I can start my research into the topic or have any friendly pointers they can offer?

x = 1
y = 0

def countodd(x):
    
    while (x<= 10):
    
        x = x+2
        print x

def counteven(y):
    
    while (y <= 10):

        y = y+2
        print y

def call():

    if(x<=10):
    
        countodd(x)

    if(y<=10):
    
        counteven(y)

call()

I am aware of 2 existing packages, parallel python and coreit, but haven't tried either. Let us know how it goes.

I went with pythons built in module "multiproccessing" to attempt to get a feel for it.

My program however manages not to do what I want and intern crash my computer however i havent got a clue why.

Can anyone help? =/

from multiprocessing import Process, Lock

x = 1
y = 0

lock = Lock()

def countodd(l, x):
    
    while (x <= 10):

        x = x+2
        
        l.acquire()
        print x
        l.release()

def counteven(l, y):


    if __name__ == '__main__':
        
        while (y <= 10):

            y = y+2

            l.acquire()
            print y
            l.release()

def call(l):
    
    p1 = Process(target = countodd, args = (l, x))
    
    p2 = Process(target = counteven, args = (l, y))

    p1.start()
    p2.start()
    p1.join()
    p2.join()

call(lock)

Seems to work OK for me. What OS are you using? Is this a typo

def counteven(l, y):


    if __name__ == '__main__':
        
        while (y <= 10):

windows 7... I know =/

Can I also ask why that's a typo, I don't see it. If its the double line space then it is a typo, but fixing it doesn't work either. =/

i was wondering what was the point of checking main in counteven at all??

To be honest I have no idea, I'm brand new to thread programming and am unsure whether it was necessary.

I'm currently researching the topic and am trying to find a beginer to pro tutorial.

Im studying programming in collage, but its only to a basic level such as if statements and for loops so as you've probably already assumed its mind numbingly simple.

All the extra work I do, i'm doing off my own back for my own benefit, so try not to be too harsh on me when I make newbie mistakes, cos I am just a newbie after all. =p

Only one function is necessary as they both do the same thing. And I remember reading about "__main__" being necessary for a certain type of code on Windows, but don't remember what is was. Anyway, try running this code from a file, not from the command line, as see if it works ("__main__" is in the correct place--it just means that the calling program is this one, as opposed to the program being called from another program).

from multiprocessing import Process, Lock

def countodd(l, x):
    
    while (x <= 10):

        x = x+2
        
        l.acquire()
        print x
        l.release()

def call(l):
    
    p1 = Process(target = countodd, args = (l, x))
    
    p2 = Process(target = countodd, args = (l, y))

    p1.start()
    p2.start()
    p1.join()
    p2.join()

if __name__ == "__main__":
    x = 1
    y = 0

    lock = Lock()

    call(lock)

Thanks for the amendment but it still crashed. Must be my OS?

Windows 7, Python 2.7x.

is your win7 32 bit??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.