Python Forum
Multiprocessing: Threads work well. Processes don't work. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Multiprocessing: Threads work well. Processes don't work. (/thread-41233.html)

Pages: 1 2


Multiprocessing: Threads work well. Processes don't work. - viyubu - Dec-02-2023

Problem with Python 3.10.12 on a notebook with an AMD C60 processor, 4 GB RAM. Ubuntu 22.04.3.
Multiprocessing:
Threads work well. Processes don't work, not even the examples from the Python documentation.
There is no error message. The target function is not executed with start().

Any help is appreciated.


RE: Multiprocessing: Threads work well. Processes don't work. - deanhystad - Dec-02-2023

Please post code.


RE: Multiprocessing: Threads work well. Processes don't work. - viyubu - Dec-02-2023

## from Pyhthon`s docs :

from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()
    print( 'main. end' )
## another example :
import concurrent.futures
import math

PRIMES = [
    112272535095293,
    112582705942171,
    112272535095293,
    115280095190773,
    115797848077099,
    1099726899285419]

def is_prime(n):
    if n < 2:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False

    sqrt_n = int(math.floor(math.sqrt(n)))
    for i in range(3, sqrt_n + 1, 2):
        if n % i == 0:
            return False
    return True

def main():
    with concurrent.futures.ProcessPoolExecutor() as executor:
        for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
            print('%d is prime: %s' % (number, prime))

if __name__ == '__main__':
    main()



RE: Multiprocessing: Threads work well. Processes don't work. - deanhystad - Dec-02-2023

Works fine for me. I' running ubuntu 22.04LTS and Python 3.11.1. What are you seeing that makes you think it doesn't work?


RE: Multiprocessing: Threads work well. Processes don't work. - snippsat - Dec-02-2023

Code works.
Here with added timing and max_workers=8(use 8 cores).
Then it should be easier to see if it work,and run from command line not any editors when this out.
import concurrent.futures
import math, time

PRIMES = [
    111111111191122,
    112582705942171,
    112272535095293,
    115280095190773,
    115797848077099,
    1099726899285419
] * 10 # Longer run time

def is_prime(n):
    if n < 2:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    sqrt_n = int(math.floor(math.sqrt(n)))
    for i in range(3, sqrt_n + 1, 2):
        if n % i == 0:
            return False
    return True

def main():
    start = time.time()
    with concurrent.futures.ProcessPoolExecutor(max_workers=8) as executor:
        for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
            print(f'{number} is prime: {prime}')
    stop = time.time()
    total = stop - start
    print(f'Time was: {total:.2f}')

if __name__ == '__main__':
    main()
Output:
..... 115280095190773 is prime: True 115797848077099 is prime: True 1099726899285419 is prime: False Time was: 6.17
If set max_workers=1 also now use only 1 core in CPU.
The time should be slower.
Output:
..... 115280095190773 is prime: True 115797848077099 is prime: True 1099726899285419 is prime: False Time was: 23.63



RE: Multiprocessing: Threads work well. Processes don't work. - viyubu - Dec-02-2023

There is no error message. The target function is not executed with start().


RE: Multiprocessing: Threads work well. Processes don't work. - deanhystad - Dec-03-2023

Quote:The target function is not executed with start().
What makes you think that?


RE: Multiprocessing: Threads work well. Processes don't work. - viyubu - Dec-03-2023

Code :

from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':    
    from time import time
    t0 = time()
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join(1) #####
    print( 'main. end' )
    print( 'Time was :', time() - t0 )
Printout :

main line
module name: __main__
parent process: 4959
process id: 5722

and a dialog box :
Kill ?
Your program is still running!
Do you want to kill it?
'Ok' 'Cancelar'

Click 'Cancelar

main line
module name: __main__
parent process: 4959
process id: 5722
main. end
Time was : 91.7859697341919


RE: Multiprocessing: Threads work well. Processes don't work. - snippsat - Dec-03-2023

(Dec-03-2023, 07:03 PM)viyubu Wrote: and a dialog box :
Kill ?
Your program is still running!
From where are you getting a dialog box?
As i mention run from command line,
this is because if run from Editor/Ide it can mess with Process like block or hang.


RE: Multiprocessing: Threads work well. Processes don't work. - viyubu - Dec-03-2023

I'm using Python's IDLE for edit and run the program.

Honestly, I am very grateful and trying to learn how to post on the forum.