如何使用非阻塞IO写入文件?

编程入门 行业动态 更新时间:2024-10-25 00:28:43
本文介绍了如何使用非阻塞IO写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在Python中使用非阻塞方法写入文件。在一些Google上,我发现语言支持 fcntl 为了这样做,但实现的方法不是很清楚。

这是代码片断(我不知道错在哪里): pre > 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 \\\)

这是对文件执行非阻塞IO操作的正确方法吗?我对此表示怀疑。此外,你可以建议在Python中的其他任何模块,这使我可以这样做吗?解决方案

这是你如何打开非阻塞在UNIX上的文件模式:

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

但是,在UNIX上,打开非阻塞模式的对于正规文件!即使文件处于非阻塞模式, os.write 调用也不会立即返回,它将一直处于睡眠状态直到写入完成。为了证明这一点,你可以尝试一下:

import os import datetime 数据=\ n.join(xrange(10000000)x中的testing \\\* 10) print(数据大小是%d个字节%len(data)) print(在%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()))

您会注意到 os.write 调用需要几个秒。即使这个调用是非阻塞的(技术上,它不是阻塞的,它正在休眠),调用是不是异步的。

AFAIK,没有办法在Linux或Windows上异步写入文件。但是,您可以使用线程来模拟它。为此,Twisted有一个名为 deferToThread 的方法。下面是你如何使用它:

from twisted.internet导入线程,reactor data =\\ \\ n.join(testing \\\* 10 for x in xrange(10000000)) print(数据大小是%d个字节%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(运行test_callback,只是为了踢脚踢) d = threads.deferToThread(blocking_code) reactor.callWhenRunning(cc) reactor.run()

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?

解决方案

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()

更多推荐

如何使用非阻塞IO写入文件?

本文发布于:2023-11-25 08:58:16,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文件   IO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!