Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:28:58+00:00 2026-06-15T11:28:58+00:00

I want to write to a file using a non-blocking method in Python. On

  • 0

I want to write to a file using a non-blocking method in Python. On some googling, I found that the language supports fcntl in order to do so, but the method to implement the same is not very clear to me.

This is the code snippet (I don’t know where I am going wrong):

import os, fcntl
nf = fcntl.fcntl(0,fcntl.F_UNCLK)
fcntl.fcntl(0,fcntl.F_SETFL , nf | os.O_NONBLOCK )
nf = open ("test.txt", 'a') 
nf.write ( " sample text \n")

Is this the correct way to perform a non-blocking IO operation on a file? I doubt it. Also, could you suggest any other modules in Python which allow me to do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:28:59+00:00Added an answer on June 15, 2026 at 11:28 am

    This is how you turn non-blocking mode on for a file in UNIX:

    fd = os.open("filename", os.O_CREAT | os.O_WRONLY | os.O_NONBLOCK)
    os.write(fd, "data")
    os.close(fd)
    

    On UNIX, however, turning on non-blocking mode has no visible effect for regular files! Even though the file is in non-blocking mode, the os.write call won’t return immediately, it will sleep until the write is complete. To prove it to yourself experimentally, try this:

    import os
    import datetime
    
    data = "\n".join("testing\n" * 10 for x in xrange(10000000))
    print("Size of data is %d bytes" % len(data))
    
    print("open at %s" % str(datetime.datetime.now()))
    fd = os.open("filename", os.O_CREAT | os.O_WRONLY | os.O_NONBLOCK)
    print("write at %s" % str(datetime.datetime.now()))
    os.write(fd, data)
    print("close at %s" % str(datetime.datetime.now()))
    os.close(fd)
    print("end at %s" % str(datetime.datetime.now()))
    

    You’ll notice that the os.write call does take several seconds. Even though the call is non-blocking (technically, it’s not blocking, it’s sleeping), the call is not asynchronous.


    AFAIK, there is no way to write to a file asynchronously on Linux or on Windows. You can simulate it, however, using threads. Twisted has a method named deferToThread for this purpose. Here’s how you use it:

    from twisted.internet import threads, reactor
    
    data = "\n".join("testing\n" * 10 for x in xrange(10000000))
    print("Size of data is %d bytes" % len(data))
    
    def blocking_write():
        print("Starting blocking_write")
        f = open("testing", "w")
        f.write(data)
        f.close()
        print("End of blocking_write")
    
    def test_callback():
        print("Running test_callback, just for kicks")
    
    d = threads.deferToThread(blocking_code)
    reactor.callWhenRunning(cc)
    reactor.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write some text directly to a file using Flex 3 /
I want to write a .xml file using the following code into the App_Data/posts.
I want to write JSON data to a text file using jQuery and PHP.
I want to write one single bit to a binary file. using (FileStream fileStream
I am using ofstream() to write data into file, i want the program to
I want to write a JAR file that makes use of the javax servlet
I want to write an application that gets events on every file change on
I am trying to write a program that read a file using fstream then,
I want to write a file on my iPhone app. My file is in
I want to write a file where an external application can read it, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.