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.
#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


Messages In This Thread
RE: Multiprocessing: Threads work well. Processes don't work. - by snippsat - Dec-02-2023, 09:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking some script design advice... involves asyncio, threads, multiprocessing sawtooth500 0 94 Jun-27-2024, 05:56 PM
Last Post: sawtooth500
  equalto validator doesnt work robertkwild 1 248 Jun-02-2024, 06:16 AM
Last Post: Pedroski55
  I can't for the life of me get this basic If statement code to work CandleType1a 8 545 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Extending list doesn't work as expected mmhmjanssen 2 381 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Using xml.parsers.expat I can't get parser.ParseFile(f) to work Pedroski55 3 453 Apr-24-2024, 07:36 AM
Last Post: Pedroski55
  Making the tab key work like a tab key jackg 3 420 Apr-16-2024, 09:02 PM
Last Post: deanhystad
  print doesnt work in a function ony 2 471 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,971 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  Pydoc documentation doesnt work Cosmosso 5 4,655 Nov-25-2023, 11:17 PM
Last Post: vidito
  hi need help to make this code work correctly atulkul1985 5 1,040 Nov-20-2023, 04:38 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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