在gevent monkey补丁中,我没有看到任何关于默认文件对象的操作。 如何在基于gevent的程序中使用异步文件读/写?
答案 0 :(得分:4)
您可以使用1.0b3中提供的gevent文件object.FileObject ThreadPool类:
pip install http://gevent.googlecode.com/files/gevent-1.0b3.tar.gz#egg=gevent
然后你的例子将成为:
#!/usr/bin/env python
import gevent
from gevent.fileobject import FileObjectThreadPool
import datetime
def hi():
while True:
print datetime.datetime.now(), "Hello"
gevent.sleep( 1 )
def w():
print "writing..."
s = "*"*(1024*1024*1024)
print 'about to open'
f_raw = open( "./a.txt", "wb" )
f = FileObjectThreadPool(f_raw, 'wb')
f.write(s)
f.close()
print 'write done'
t1 = gevent.spawn(hi)
t2 = gevent.spawn(w)
ts = [t1,t2]
gevent.joinall( ts )
我看到以下输出代码:
writing...
about to open
2012-08-13 13:00:27.876202 Hello
2012-08-13 13:00:28.881119 Hello
2012-08-13 13:00:29.959642 Hello
...
2012-08-13 13:00:58.010001 Hello
2012-08-13 13:00:59.010146 Hello
2012-08-13 13:01:00.010248 Hello
write done
2012-08-13 13:01:01.469547 Hello
...
答案 1 :(得分:1)
刚做了一个测试,说写一个大文件会阻塞事件循环
#!/usr/bin/env python
import gevent
import datetime
def hi():
while True:
print datetime.datetime.now(), "Hello"
gevent.sleep( 1 )
def w():
print "writing..."
s = "*"*(1024*1024*1024)
f = open( "e:/a.txt", "wb" )
f.write(s)
f.close()
t1 = gevent.spawn(hi)
t2 = gevent.spawn(w)
ts = [t1,t2]
gevent.joinall( ts )
结果如下:
e:\zPython\zTest>gevent.write.large.file.py
writing... # wait a long time here
write done.
2012-07-16 09:53:23.784000 Hello
2012-07-16 09:53:24.786000 Hello
2012-07-16 09:53:25.788000 Hello
答案 2 :(得分:0)
您可以使用线程池(以gevent 1.0开头):
>>> import gevent.threadpool
>>> pool = gevent.threadpool.ThreadPool(5)
>>> pool.apply(w)