“做这个,当你完成后回来” [英] "Do this, and come back when you're done"

查看:55
本文介绍了“做这个,当你完成后回来”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能,可以为

本地和远程服务器上的文件生成MD5哈希值。远程服务器有一个小applet,它从$ ine运行,并根据文件名生成一个MD5哈希。


问题是需要2分钟以上要生成MD5哈希,所以

每次调用此函数大约需要5分钟。由于

第一个MD5哈希是在远程计算机上生成的,因此本地计算机确实只需等待一半的时间。


有没有办法重写每一半函数在

背景中运行,可以这么说,然后有一个主进程等待结果

结果?这会将执行时间减少一半或更少。


#checkMD5

def checkMD5(fileName,localDir):

#获取远程哈希

Socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

Socket.connect((MD5server,888))

#throw away ID string

Socket.recv(256)

Socket.send(fileName +''\ n'')

remoteMD5hash = Socket.recv(256)

#get local hash

try:

file = open(makeMovieName(localDir,fileName) ),''r'')

除了IOError:

localMD5hash =''0''

else:

hasher = md5.new()

而True:

chunk = file.read(1024)

如果不是块:

break

hasher.update(chunk)

localMD5hash = hasher.hexdigest()

如果Debug:print" local:",localMD5hash," remote:",remoteMD5hash

return localMD5hash.strip()== remoteMD5hash.strip()


-Kamus


-

o__ |如果你老了,就吃对了,骑一辆像样的自行车。

,> /''_ |问:

(_)\(_)| Usenet发帖`

I have the following function which generates MD5 hashes for files on a
local and remote server. The remote server has a little applet that
runs from inetd and generates an MD5 hash given the file name.

The problem is that it takes 2+ minutes to generate the MD5 hash, so
this function takes about 5 minutes every time it is called. Since the
first MD5 hash is generated on a remote machine, the local machine does
nothing but wait for half that time.

Is there any way to rewrite each half of the function to run in the
background, so to speak, and then have a master process that waits on
the results? This would cut execution time in half more or less.

# checkMD5
def checkMD5(fileName, localDir):
# get remote hash
Socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Socket.connect((MD5server,888))
#throw away ID string
Socket.recv(256)
Socket.send(fileName+''\n'')
remoteMD5hash = Socket.recv(256)

# get local hash
try:
file=open(makeMovieName(localDir,fileName), ''r'')
except IOError:
localMD5hash = ''0''
else:
hasher = md5.new()
while True:
chunk = file.read(1024)
if not chunk:
break
hasher.update(chunk)
localMD5hash = hasher.hexdigest()
if Debug: print "local:",localMD5hash, "remote:",remoteMD5hash
return localMD5hash.strip() == remoteMD5hash.strip()

-Kamus

--
o__ | If you''re old, eat right and ride a decent bike.
,>/''_ | Q.
(_)\(_) | Usenet posting`

推荐答案

Kadizhar的Kamus< ya*@NsOeSiPnAeMr.com>写道:
Kamus of Kadizhar <ya*@NsOeSiPnAeMr.com> writes:
有没有办法重写函数的每一半在
后台运行,可以这么说,然后有一个等待结果的主进程结果?这会将执行时间减少一半或更少。
Is there any way to rewrite each half of the function to run in the
background, so to speak, and then have a master process that waits on
the results? This would cut execution time in half more or less.




当然,请使用线程模块。考虑一下你正在做什么的另一个方面。你正在比较同一文件的本地和远程

副本的md5',看看它们是否相同。您是否试图检测恶意篡改?b $ b检测恶意篡改?如果有人篡改了

文件中的一个,你怎么知道那个人也不能拦截你的网络

连接并向你发送正确的信息。 md5,所以你不会发现

篡改?或者就此而言,你知道程序本身的远程拷贝没有被篡改吗?



Sure, use the threading module. Think about another aspect of what
you''re doing though. You''re comparing the md5''s of a local and remote
copy of the same file, to see if they''re the same. Are you trying to
detect malicious tampering? If someone tampered with one of the
files, how do you know that person can''t also intercept your network
connection and send you the "correct" md5, so you won''t detect the
tampering? Or for that matter, do you know that the remote copy of
the program itself hasn''t been tampered with?


Paul Rubin写道:
Paul Rubin wrote:
Kadizhar的Kamus< ya*@NsOeSiPnAeMr.com>写道:
Kamus of Kadizhar <ya*@NsOeSiPnAeMr.com> writes:
有没有办法重写函数的每一半在
后台运行,可以这么说,然后有一个等待的主进程
当然,请使用线程模块。
Is there any way to rewrite each half of the function to run in the
background, so to speak, and then have a master process that waits on
the results? This would cut execution time in half more or less.

Sure, use the threading module.




好​​的,我会读完在那。我已经写了大量的科学类型代码,

但这个OS对我来说是新的。

想想你的另一个方面
你是尽管如此。你正在比较同一文件的本地和远程
副本的md5',看看它们是否相同。您是否正在尝试检测恶意篡改?



OK, I''ll read up on that. I''ve written gobs of scientific type code,
but this OS stuff is new to me.
Think about another aspect of what
you''re doing though. You''re comparing the md5''s of a local and remote
copy of the same file, to see if they''re the same. Are you trying to
detect malicious tampering?




不,实际上,这两台机器都在我的控制之下(在我家里)。我是在一个不可靠,速度慢的无线网络上投放大量(1GB MOL)文件的b
$ b。我试图通过网络检测到不完整的副本。

本地计算机是视频播放器,远程计算机是

存档服务器。我的孩子习惯只关闭视频

服务器,导致档案转移不完整。


如果它适合这个新闻组,我想发布整个

的评论(这是我的第一段pyton代码。)到目前为止,python

是最简单的学习的语言我曾经遇到过。我试过学习perl,这是一场灾难......太复杂了。 Python是一股新鲜空气。此外,这里的文档和支持非常好。

:-)感谢所有及时建立python的志愿者。


-Kamus < br $> b $ b -

o__ |如果你老了,就吃对了,骑一辆像样的自行车。

,> /''_ |问:

(_)\(_)| Usenet发帖`



No, actually, both machines are under my control (and in my house). I''m
slinging large (1GB MOL) files around on an unreliable, slow wireless
network. I am trying to detect an incomplete copy across the network.
The local machine is the video player and the remote machine is the
archive server. My kids have a habit of just shutting down the video
server, resulting in incomplete transfers to the archives.

If it''s appropriate for this newsgroup, I''d like to post the entire
effort for comments (it''s my first bit of pyton code.) So far, python
has been the easiest language to learn I''ve ever come across. I tried
learning perl, and it was a disaster.... Too convoluted. Python is a
breath of fresh air. Also, the docs and support here is excellent.
:-) My thanks to all the volunteers who put in time to build python.

-Kamus
--
o__ | If you''re old, eat right and ride a decent bike.
,>/''_ | Q.
(_)\(_) | Usenet posting`


Kamad of Kadizhar< ya*@NsOeSiPnAeMr.com>写道:
Kamus of Kadizhar <ya*@NsOeSiPnAeMr.com> writes:
不,实际上,两台机器都在我的控制之下(在我家里)。
我正在以不可靠,缓慢的方式投放大型(1GB MOL)文件无线网络。我试图通过
网络检测到不完整的副本。本地计算机是视频播放器,远程计算机是存档服务器。我的孩子习惯只关闭
视频服务器,导致转移到档案。如果它适合这个新闻组,我想发表评论的全部工作(这是我的第一部分pyton代码。)到目前为止,python
一直是最容易学习的语言我曾经遇到过。我试过学习perl,这是一场灾难....太复杂了。 Python是一股清新的空气。此外,这里的文档和支持非常好。 :-)感谢所有及时建立python的志愿者。
No, actually, both machines are under my control (and in my house).
I''m slinging large (1GB MOL) files around on an unreliable, slow
wireless network. I am trying to detect an incomplete copy across the
network. The local machine is the video player and the remote machine
is the archive server. My kids have a habit of just shutting down the
video server, resulting in incomplete transfers to the archives. If
it''s appropriate for this newsgroup, I''d like to post the entire
effort for comments (it''s my first bit of pyton code.) So far, python
has been the easiest language to learn I''ve ever come across. I tried
learning perl, and it was a disaster.... Too convoluted. Python is a
breath of fresh air. Also, the docs and support here is
excellent. :-) My thanks to all the volunteers who put in time to
build python.




为什么不看看rsync程序。它通过自动检测

文件之间的差异并仅通过网络发送增量来使两台机器相互同步。



Why don''t you look at the rsync program. It brings two machines into
sync with each other by automatically detecting differences between
files and sending only the deltas over the network.


这篇关于“做这个,当你完成后回来”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆