我需要一些方法来在pool.map()中使用一个接受多个参数的函数.根据我的理解,pool.map()的目标函数只能有一个iterable作为参数,但有没有办法可以传递其他参数?在这种情况下,我需要传递一些配置变量,比如我的Lock()和记录信息到目标函数.
我试图做一些研究,我认为我可以使用部分功能来使其工作?但是我不完全理解这些是如何工作的.任何帮助将不胜感激!这是我想要做的一个简单示例:
def target(items, lock):
for item in items:
# Do cool stuff
if (... some condition here ...):
lock.acquire()
# Write to stdout or logfile, etc.
lock.release()
def main():
iterable = [1, 2, 3, 4, 5]
pool = multiprocessing.Pool()
pool.map(target(PASS PARAMS HERE), iterable)
pool.close()
pool.join()
Run Code Online (Sandbox Code Playgroud) 我试图使用部分函数,以便pool.map()可以定位具有多个参数的函数(在本例中为Lock()对象).
这是示例代码(取自我之前的一个问题的答案):
from functools import partial
def target(lock, iterable_item):
for item in items:
# Do cool stuff
if (... some condition here ...):
lock.acquire()
# Write to stdout or logfile, etc.
lock.release()
def main():
iterable = [1, 2, 3, 4, 5]
pool = multiprocessing.Pool()
l = multiprocessing.Lock()
func = partial(target, l)
pool.map(func, iterable)
pool.close()
pool.join()
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,我收到错误:
Runtime Error: Lock objects should only be shared between processes through inheritance.
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?如何在子进程之间共享锁?
我做了尽可能多的研究但是我没有找到最好的方法来使某些cmdline参数仅在某些条件下是必要的,在这种情况下只有在给出其他参数的情况下.这是我想要在一个非常基本的层面上做的事情:
p = argparse.ArgumentParser(description='...')
p.add_argument('--argument', required=False)
p.add_argument('-a', required=False) # only required if --argument is given
p.add_argument('-b', required=False) # only required if --argument is given
Run Code Online (Sandbox Code Playgroud)
从我所看到的,其他人似乎只是在最后添加自己的支票:
if args.argument and (args.a is None or args.b is None):
# raise argparse error here
Run Code Online (Sandbox Code Playgroud)
有没有办法在argparse包中本地执行此操作?
使用Multiprocessing.Pool()快速询问有关多个进程之间的共享变量的问题.
如果我从多个进程中更新全局列表,我会遇到任何问题吗?即如果其中两个进程试图同时更新列表.
我已经看过有关使用Lock进行类似操作的文档,但我想知道是否有必要.
编辑:
我分享这个变量的方法是在我的回调函数中使用一个全局变量,'successcesses',其中我在目标函数完成后附加了所有成功的动作:
TOTAL_SUCCESSES = []
def func(inputs):
successes = []
for input in inputs:
result = #something with return code
if result == 0:
successes.append(input)
return successes
def callback(successes):
global TOTAL_SUCCESSES
for entry in successes:
TOTAL_SUCCESSES.append(entry)
def main():
pool = mp.Pool()
for entry in myInputs:
pool.apply_async(func, args=(entry,),callback=callback)
Run Code Online (Sandbox Code Playgroud)
为任何语法错误道歉,快速写下来,但程序正在工作只是想知道如果我有问题我是否添加共享变量.
提前致谢!
我正在编写一个小脚本来串行遍历目录并在其中的子目录上运行命令.
我遇到了一个问题,但是Popen()它将遍历目录并运行所需的命令而无需等待前一个完成.即
for dir in dirs:
#run command on the directory here.
Run Code Online (Sandbox Code Playgroud)
它开始了每个目录的命令而不关心它.我希望它等待当前的一个完成,然后启动下一个.我在目录上使用的工具Log2timeline来自SANS SIFT,这需要相当长的时间并产生相当多的输出.我不关心输出,我只是希望程序在开始下一个之前等待.
实现这一目标的最佳方法是什么?
谢谢!
我有一个参数是一个1-100的整数的程序,我只是不喜欢它在使用argparse时显示在-h帮助消息中的方式(它字面上列出0,1,2,3,4 ,5,......等)
有没有办法改变这个或用另一种方式代表它?
谢谢
编辑:
以下是那些询问的人的代码:
norse = parser.add_argument_group('Norse')
norse.add_argument('-n', '--norse', required=False, help='Run the Norse IPViking scan.', action='store_true')
norse.add_argument('--threshold', required=False, type=int, choices=range(0,101), help='Threshold (0-100) denoting at what threat level to provide additional data on an IP \
address. Default is 49.', default=49)
Run Code Online (Sandbox Code Playgroud) 最近,我在 Python 中打开特定的 UTF-16 编码文件时遇到问题。我已经尝试过以下方法:
import codecs
f = codecs.open('filename.data', 'r', 'utf-16-be')
contents = f.read()
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
UnicodeDecodeError: 'utf16' codec can't decode bytes in position 18-19: illegal UTF-16 surrogate
Run Code Online (Sandbox Code Playgroud)
尝试读取文件的内容后。我也尝试过强制使用小尾数,但这不好。文件头如下:
0x FE FF EE FF
Run Code Online (Sandbox Code Playgroud)
我读过的表示 UTF-16 Big Endian。我已经能够使用以下命令将文件的内容读入原始字符串:
f = open('filename.data', 'rb')
raw = f.read()
hex = binascii.hexlify(raw)
Run Code Online (Sandbox Code Playgroud)
这可以让我获得原始十六进制,但问题是 - 有时这些文件是小端,有时它们是大端,所以我本质上只是想在开始解析之前规范化数据,我希望编解码器会这样做能够帮助我,但没有运气..
有人知道这里发生了什么事吗?我会提供文件作为参考,但有一些敏感数据,所以不幸的是我不能。该文件由 Windows 操作系统使用。
正如我上面提到的,我的最终目标是能够打开/读取这些文件并规范化它们,以便我可以对所有这些文件使用相同的解析器,而不是必须编写一些带有一堆错误处理的解析器如果编码很古怪。
编辑:根据要求,文件的前 32 个字节:
FE FF EE FF 11 22 00 00 03 00 00 00 01 00 00 00
92 EC DA 48 1B 00 …Run Code Online (Sandbox Code Playgroud) 我试图在python中使用和操作subprocess.check_output()的输出,但因为它是逐字节返回的
for line in output:
# Do stuff
Run Code Online (Sandbox Code Playgroud)
不行.有没有办法可以将输出重建为打印到stdout时的行格式?或者搜索并使用此输出的最佳方法是什么?
提前致谢!
我在使用时遇到一些奇怪的问题subprocess.check_output()。起初我只是使用subprocess.call(),一切正常。然而,当我简单地切换到 时call(),check_output()我收到一个奇怪的错误。
代码之前(工作正常):
def execute(hosts):
''' Using psexec, execute the batch script on the list of hosts '''
successes = []
wd = r'c:\\'
file = r'c:\\script.exe'
for host in hosts:
res = subprocess.call(shlex.split(r'psexec \\\\%s -e -s -d -w %s %s' % (host,wd,file)))
if res.... # Want to check the output here
successes.append(host)
return successes
Run Code Online (Sandbox Code Playgroud)
代码之后(不起作用):
def execute(hosts):
''' Using psexec, execute the batch script on the list of hosts '''
successes …Run Code Online (Sandbox Code Playgroud) 我正在使用subprocess.check_output()通过Sysinternals PSExec在远程计算机上执行一些命令。我遇到的问题是,如果命令给出非零的返回码,则check_output()会引发CalledProcessError。
PSExec将创建的进程的PID作为其返回代码,因此很明显每次都会出现错误。有没有一种方法可以覆盖此错误并仍然检查psexec调用的输出?基本上,我只需要查看输出,即使其返回码为非零也是如此。
谢谢!