Python Forum
Multiprocessing: Threads work well. Processes don't work.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiprocessing: Threads work well. Processes don't work.
#1
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.
Reply
#2
Please post code.
Reply
#3
## 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()
Gribouillis write Dec-02-2023, 06:01 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#4
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?
Reply
#5
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
Reply
#6
There is no error message. The target function is not executed with start().
Reply
#7
Quote:The target function is not executed with start().
What makes you think that?
Reply
#8
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
Reply
#9
(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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 231 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Using xml.parsers.expat I can't get parser.ParseFile(f) to work Pedroski55 3 305 Apr-24-2024, 07:36 AM
Last Post: Pedroski55
  Making the tab key work like a tab key jackg 3 325 Apr-16-2024, 09:02 PM
Last Post: deanhystad
  print doesnt work in a function ony 2 368 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Need some help trying to get my Python mortgage amortization calculator to work prope IamSirAskAlot 4 15,838 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  Pydoc documentation doesnt work Cosmosso 5 4,473 Nov-25-2023, 11:17 PM
Last Post: vidito
  hi need help to make this code work correctly atulkul1985 5 880 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  How to copy work sheet data one workbook to other? sayyedkamran 2 747 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  newbie question - can't make code work tronic72 2 739 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why wont this path work one way, but will the other way? cubangt 2 704 Sep-01-2023, 04:14 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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