1

In my development, I have shared file which need to write mutliple process but I found it write only header part(only first line i.e test_data). I am able to create same behavior using following script. Any Input what am I missing?

#! /usr/bin/env python

import multiprocessing

def check_proc(procs):
    """checking cpu utilization befor starting process"""
    for proc in procs:
        proc.start()

    while(len(multiprocessing.active_children())>10):
        continue

    for proc in procs:
        proc.join()

    while (len(multiprocessing.active_children())>5):
        time.sleep(1)

def create_file():
    f_data = open("temp", "w")
    f_data.write("test data\n")
    return f_data

def write_data(f_data, number, lock):
    lock.acquire()
    f_data.write(number)
    lock.release()

def main():
    f_data = create_file()
    procs = []
    lock = multiprocessing.Lock()
    for i in range(0, 20, 1):
        proc = multiprocessing.Process(target=write_data,
                                       args=(f_data, str(i), lock))
        procs.append(proc)
    check_proc(procs)


if __name__ == "__main__":
    main()

Expected: it should have number

Python2.7 and linux enviroment

I have gone through stack overflow question and block and found following link. But I am not able to relate or use in above code. Any input.

http://ptspts.blogspot.com/2013/08/how-to-send-unix-file-descriptors.html

user765443
  • 1,856
  • 7
  • 31
  • 56
  • `while(len...` is a busy loop. Add `time.sleep()` between iterations. – ivan_pozdeev Jun 05 '18 at 13:58
  • _"but I found it write only header part"_ - could you clarify? What is the expected and actual output? – ivan_pozdeev Jun 05 '18 at 13:59
  • I think this is a question about sharing file descriptors between multiple processes. And there are some related questions, for example: https://stackoverflow.com/questions/2989823/how-to-pass-file-descriptors-from-parent-to-child-in-python – Sraw Jun 05 '18 at 14:03
  • Possible duplicate of [How to pass file descriptors from parent to child in python?](https://stackoverflow.com/questions/2989823/how-to-pass-file-descriptors-from-parent-to-child-in-python) – ivan_pozdeev Jun 05 '18 at 14:16
  • What [start method](https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods) are you using? (That is, what version of Python is this, and on what platform?) – Davis Herring Jun 05 '18 at 16:06
  • it means, we can not handle such case. – user765443 Jun 05 '18 at 16:08

0 Answers0