CGI和文件排除 [英] CGIs and file exclusion
问题描述
大家好,
在做一套相当大的程序时对于一所大学科目我已经发现自己处于以前肯定有过的问题中,所以
我正在寻求帮助。 />
有一次,我调用python cgi来pickle.load'的文件,添加或删除
a注册表并将结果再次转储到文件中。 br />
我担心可以从两台或多台不同的计算机同时调用cgi,因此最有可能破坏文件。我不认为
我可以使用互斥锁,因为它是程序的两个不同实例,而不是
不同的线程,所以我在想锁定
程序之间的文件,但我真的不知道该怎么做。如果
没有可行的方法,那就不是问题了,它只能在基于linux的基于b $ b的计算机上运行。
我接受的另一个解决方案是,第二个叫做cgi的人检测到其他实例正在运行并显示一条消息,说明稍后再试。
是的,不太专业,但是它会完成这项工作,毕竟这只是一个小小的细节,我想找一个相当挑剔的教授,而不是一个真正的
life"事情。
我认为'你需要的所有背景,如果有人可以用
回答我应该寻找什么,或者更好的示例代码
简直太棒了。
非常感谢提前。
DH
我建议使用ZODB而不是直接酸洗 - 然后每个cgi都可以
用自己的连接打开数据库。 ZODB将管理并发
问题。
-
问候,
Diez B. Roggisch
" darkhorse" <无************ @ terra.es>写道:
大家好,
同时做一个非常大的程序集对于一个大学科目我已经发现自己正处于一个肯定有过的问题中,所以
我正在寻求一些帮助。
在一个点,我调用python cgi,pickle.load'的文件,添加或删除注册表并将结果再次转储到文件中。
我担心可以同时调用cgi来自两个或多个不同的计算机,因此很可能破坏文件。我不认为
我可以使用互斥锁,因为它是程序的两个不同实例而不是不同的线程,所以我在考虑将文件锁定在
程序之间,但我真的不知道该怎么做。这不是一个问题,如果没有可移植的方式,它只会在基于Linux的计算机上运行。
你想要fcntl模块,并使用flock()或lockf()。
import fcntl,cPickle
#在更新模式下打开
fo = open(''somefile.pickle'',''r +'')
#获得一个独占锁;任何其他尝试获得此过程的过程
#将阻止
fcntl.lockf(fo.fileno(),fcntl.LOCK_EX)
data = cPickle.load(fo)
....做数据的东西...
#回到开头
fo.seek (0)
#写出新的泡菜
cPickle.dump(数据,fo)
#抛出其余的(旧的)腌渍
fo.truncate()
#并关闭。这释放了锁。
fo.close()
我接受的另一个解决方案是第二个调用cgi检测到
其他实例正在运行并显示消息说要再试一次。
是的,不太专业,但是它会做这个工作,毕竟这只是一个小小的细节,我想满足于一个非常挑剔的教授,而不是一个真实的生活 $。
b b b b b b b b b b b b b fcntl.LOCK_NB作为fcntl.lockf()的标志。如果无法获得
锁定,则会引发IOError,并且您可以打印出消息中的
。
请注意,lockf()会进行咨询锁定;一个只打开
文件并在不调用lockf()的情况下执行某些操作的进程*不会被阻止。
-
|> | \ / |<
/ ------------------------- ------------------------------------------------- \\
| David M. Cooke
| cookedm(at)physics(dot)mcmaster(dot)ca
Diez B. Roggisch <德********* @ web.de>写道:
我建议使用ZODB而不是直接酸洗 - 然后每个cgi都可以用自己的连接打开数据库。 ZODB将管理并发性问题。
神圣的莫利,不是推荐涡轮增压器,
12-汽缸,空调SUV作为手机充电器?
当然,Zope就是答案,但只有当你非常非常地说出这个问题时才会非常小心b $ b 。
-
- Tim Roberts, ti**@probo.com
Providenza& Boekelheide,Inc。
Hi all,
While doing a quite big "set of programs" for a university subject I''ve
found myself in the middle of a problem someone surely has had before, so
I''m looking for some help.
At one point, I call a python cgi that pickle.load''s a file, adds or deletes
a registry and dumps the result again in the file.
I''m worried that the cgi could be called simultaneously from two or more
different computers, thus most probably corrupting the files. I don''t think
I can use a mutex as it''s two different instances of the program and not
different threads, so I was thinking about locking the files between
programs, but I really don''t know how to do that. It''s not a problem if
there''s no portable way of doing this, it''s only going to be run on a linux
based computer.
Another solution I would accept is that the second called cgi detects that
other instance is running and displays a message saying to try again later.
Yes, not quite professional, but it''d do the job, after all this is just a
little detail I want to settle for a quite picky professor and not a "real
life" thing.
I think that''s all the background you need, if someone can answer with
references on what should I look for or even better example code that would
be simply great.
Many thanks in advance.
DH解决方案I''d suggest using ZODB instead of directly pickling - then every cgi can
open the database with its own connection. ZODB will manage concurrency
issues.
--
Regards,
Diez B. Roggisch
"darkhorse" <no************@terra.es> writes:
Hi all,
While doing a quite big "set of programs" for a university subject I''ve
found myself in the middle of a problem someone surely has had before, so
I''m looking for some help.
At one point, I call a python cgi that pickle.load''s a file, adds or deletes
a registry and dumps the result again in the file.
I''m worried that the cgi could be called simultaneously from two or more
different computers, thus most probably corrupting the files. I don''t think
I can use a mutex as it''s two different instances of the program and not
different threads, so I was thinking about locking the files between
programs, but I really don''t know how to do that. It''s not a problem if
there''s no portable way of doing this, it''s only going to be run on a linux
based computer.
You want the fcntl module, and use either flock() or lockf().
import fcntl, cPickle
# open in update mode
fo = open(''somefile.pickle'', ''r+'')
# acquire an exclusive lock; any other process trying to acquire this
# will block
fcntl.lockf(fo.fileno(), fcntl.LOCK_EX)
data = cPickle.load(fo)
.... do stuff to data ...
# go back to the beginning
fo.seek(0)
# write out the new pickle
cPickle.dump(data, fo)
# throw the rest of the (old) pickle away
fo.truncate()
# and close. This releases the lock.
fo.close()
Another solution I would accept is that the second called cgi detects that
other instance is running and displays a message saying to try again later.
Yes, not quite professional, but it''d do the job, after all this is just a
little detail I want to settle for a quite picky professor and not a "real
life" thing.
If you want to do something like that, use
fcntl.LOCK_EX | fcntl.LOCK_NB as the flags to fcntl.lockf(). If the
lock can''t be acquired, an IOError will be raised, and you can print
out your message.
Note that lockf() does advisory locks; a process that just opens the
file and does something with it without calling lockf() *won''t* be blocked.
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
"Diez B. Roggisch" <de*********@web.de> wrote:
I''d suggest using ZODB instead of directly pickling - then every cgi can
open the database with its own connection. ZODB will manage concurrency
issues.
Holy moley, isn''t that something like recommending a turbocharged,
12-cylinder, air-conditioned SUV as a cell phone charger?
Sure, Zope is the answer, but only if you phrase the question very, very
carefully.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
这篇关于CGI和文件排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!