Python Forum
Using multiprocessing to produce objects for i in range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using multiprocessing to produce objects for i in range
#2
This should work on linux, but it will not work on windows. In linux a process is forked, starting out with the same process image as the parent. In windows a process is spawned, starting out as a completely new process.

To have a variable that is the same for all processes, you need to pass it as an argument to the process.

From the docs: https://docs.python.org/3/library/multiprocessing.html
from multiprocessing import Process, Value, Array

#Data can be stored in a shared memory map using Value or Array. For example, the following code

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print(num.value)
    print(arr[:])
Or you can use a process manager. From the same document.
from multiprocessing import Process, Manager

def f(d, l):
    d[1] = '1'
    d['2'] = 2
    d[0.25] = None
    l.reverse()

if __name__ == '__main__':
    with Manager() as manager:
        d = manager.dict()
        l = manager.list(range(10))

        p = Process(target=f, args=(d, l))
        p.start()
        p.join()

        print(d)
        print(l)
Reply


Messages In This Thread
RE: Using multiprocessing to produce objects for i in range - by deanhystad - Feb-02-2022, 01:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Produce One file Per PurchaseOrder jland47 1 420 Jan-26-2024, 11:38 AM
Last Post: Larz60+
  matplotlib x axis range goes over the set range Pedroski55 5 3,374 Nov-21-2021, 08:40 AM
Last Post: paul18fr
Photo multiprocessing with objects? - help m3atwad 0 1,309 Nov-17-2020, 03:16 AM
Last Post: m3atwad
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,362 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Can the comments produce errors in python? newbieAuggie2019 9 4,502 Nov-26-2019, 12:19 AM
Last Post: micseydel
  Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows ChrisPy33 3 3,323 Jun-12-2019, 05:56 AM
Last Post: ChrisPy33
  \t produce eight gap but tab only produce four gap liuzhiheng 3 2,472 Jun-09-2019, 07:05 PM
Last Post: Gribouillis
  Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu sultan 2 2,617 May-22-2019, 07:20 AM
Last Post: buran
  Convert file sizes: will this produce accurate results? RickyWilson 2 8,256 Dec-04-2017, 03:36 PM
Last Post: snippsat
  How can I produce a list of n times the same element? JoeB 6 3,865 Nov-27-2017, 10:40 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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