Multiprocessing write json

Hello everyone
i want to use multiprocess module and i want to write one json file but did not work good.
i tried to run one process only that work well but run with other process raise excception json decode error.

code:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
import time
from multiprocessing import Process
import json

def fun1():
while True:
with open(“ram.json”,“r”) as wrx:
alr=
alr=json.load(wrx)

    with open("ram.json","w") as wrx:
        al={}
        al["fun1"]="bunufun1 yazdı..."
        alr.append(al)
        json.dump(alr,wrx)

def fun2():

while True:
    with open("ram.json", "r") as wrx:
        alr = []
        alr = json.load(wrx)

    with open("ram.json", "w") as wrx:
        al = {}
        al["fun2"] = "bunufun2 yazdı..."
        alr.append(al)
        json.dump(alr, wrx)

def fun3():
while True:
with open(“ram.json”, “r”) as wrx:
alr =
alr = json.load(wrx)

    with open("ram.json", "w") as wrx:
        al = {}
        al["fun3"] = "bunufun3 yazdı..."
        alr.append(al)
        json.dump(alr, wrx)

def fun4():
while True:
with open(“ram.json”, “r”) as wrx:
alr =
alr = json.load(wrx)

    with open("ram.json", "w") as wrx:
        al = {}
        al["fun4"] = "bunufun4 yazdı..."
        alr.append(al)
        json.dump(alr, wrx)

def fun5():
while True:
with open(“ram.json”,“r”) as wrx:
al=
al=json.load(wrx)
print(al)

def main():
p1 = Process(target=fun1)
p2 = Process(target=fun2)
p3 = Process(target=fun3)
p4 = Process(target=fun4)
p5 = Process(target=fun5)
p1.start()
p2.start()
p3.start()
p4.start()

if name == “main”:
main()
code::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Welcome!

Its unclear to me what you are actually intending to do here. Having a bunch of programs simultaneously reading and writing from the same file, without being careful and knowing exactly what you are doing, is likely to end one of three ways:

  • You get garbled output, which ends up as garbled input, and causes your program to error out
  • Your programs deadlock and hang as one gets a lock on a file and doesn’t let go
  • Or related, your programs exit with an permissions OSError error due to the file being locked by one of them (common on Windows)

What is likely to happen is two programs are writing data to the file at the same time, or reading from it while it is being written, which is just going to be garbled nonsense. Or, what seems to be happening here, the file is blank when the program expects it to be a valid JSON, and you get an error.

In any case, you need to very clearly define the task you are trying to accomplish, your overall design for how your workers will accomplish it, and what output you expect to get.

Also, some other notes. please remember to format your code correctly, so we can actually read it—indentation is critical in Python. Like this:

```python
# YOUR CODE HERE
```

It should look like this; use the handy preview to the right to check.

# YOUR CODE HERE

Also, please try to name your functions something less cryptic than funN(), as that makes it much more difficult for us to understand what you intend them to do (comments and docstrings are even better). Similarly, providing the full error traceback will help us figure out what’s going on.

Best of luck!