I wrote the following code to understand how nonblocking write is operated:
import os, time
def takeAnap():
print('I am sleeping a bit while it is writing!')
time.sleep(50)
fd = os.open('t.txt', os.O_CREAT | os.O_NONBLOCK)
for i in range(100):
# Non-blocking write
fd = os.open('t.txt', os.O_APPEND | os.O_WRONLY | os.O_NONBLOCK)
os.write(fd, str(i))
os.close(fd)
time.sleep(2)
takeAnap()
As you can see, I am creating takeAnap()
to be activated while the loop is being processed so that I can convince my self that the writing is performed without blocking! However, the loop still blocks and the method is not performed until finishing. I am not sure if my understanding is wrong but as far as I know, non-blocking operation allows you to do other tasks while the writing is being processed. Is that correct? If so, kindly where is the problem in my code!
Thank you.