Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: is os.read() / os.write() on an os.pipe() threadsafe?
    text
    copied!<p>Consider:</p> <pre><code>pipe_read, pipe_write = os.pipe() </code></pre> <p>Now, I would like to know two things:</p> <p><strong>(1)</strong> I have two threads. If I guarantee that only one is reading <code>os.read(pipe_read,n)</code> and the other is only writing <code>os.write(pipe_write)</code>, will I have any problem, even if the two threads do it simultaneously? Will I get all data that was written in the correct order? What happens if they do it simultaneously? Is it possible that a single write is read in pieces, like?:</p> <pre><code>Thread 1: os.write(pipe_write, '1234567') Thread 2: os.read(pipe_read,big_number) --&gt; '123' Thread 2: os.read(pipe_read,big_number) --&gt; '4567' </code></pre> <p>Or -- again, consider simultaneity -- will a single <code>os.write(some_string)</code> always return entirely by a single <code>os.read(pipe_read, very_big_number)</code>?</p> <p><strong>(2)</strong> Consider more than one thread writing to the <code>pipe_write</code> end of the pipe using <code>logging.handlers.FileHandler()</code> -- I've read that the logging module is threadsafe. Does this mean that I can do this without losing data? I think I won't be able to control the order of the data in the pipe; but this is not a requirement. Requirements:</p> <ul> <li>all data written by some threads on the write end <strong>must</strong> come out at the read end </li> <li>a string written by a single <code>logger.info(), logger.error(), ...</code> has to stay in one piece.</li> </ul> <p>Are these reqs fulfilled?</p> <p>Thank you in advance,</p> <p>Jan-Philip Gehrcke</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload