我有这个代码:
import multiprocessing as mp
import shutil
import md5
def f(src,dst):
shutil.copy2(src,dst)
file_1 = "C:\Users\Nick\Documents\production\TEST\\test.txt"
file_2 = "C:\Users\Nick\Documents\production\TEST_2\\test.txt"
def get_md5(file_name):
with open(file_name) as file_to_check:
# read contents of the file
data = file_to_check.read()
# pipe contents of the file through
md5_returned = md5.new(data).hexdigest()
print md5_returned
if __name__ == '__main__':
P = mp.Process(target=f, args=(file_1,file_2))
s = mp.Process(target=get_md5, args=(file_1))
P.start()
P.join()
s.start()
s.join()
Run Code Online (Sandbox Code Playgroud)
我现在只测试如何使用多处理,但get_md5函数会抛出类型错误.错误信息是这样的:
Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "C:\Python27\lib\multiprocessing\process.py", line 114, in run …Run Code Online (Sandbox Code Playgroud) 我正在尝试用C语言从用户输入中获取字符串,以便程序可以打开选定的文件。
我尝试使用fgets是因为我在许多线程上读到它是更安全的选择(而不是gets)。
但是,当使用gets存储字符串时,文件将打开,而使用fgets则不会。
这是我正在使用的代码:
char csvFile[256];
FILE *inpfile;
printf("Please enter CSV filename: ");
fgets(csvFile,256,stdin);
printf("\nFile is %s\n",csvFile);
inpfile = fopen(csvFile,"r");
if(inpfile == NULL)
{
printf("File cannot be opened!");
}
Run Code Online (Sandbox Code Playgroud)
我知道该文件存在,但是使用fgets输入了if块。
唯一的区别是使用:
gets(csvFile);
Run Code Online (Sandbox Code Playgroud)
代替
fgets(csvFile,256,stdin);
Run Code Online (Sandbox Code Playgroud)
谁能帮我理解这一点?提前致谢。
我在bash脚本中有一个函数,如果进程正在运行,我想返回true,否则返回false.我的功能代码是:
checkProcess() {
if [ kill -s 0 $(getPid) > /dev/null 2>&1 ]; then
return 0
else
return 1
fi
}
Run Code Online (Sandbox Code Playgroud)
当我使用此功能时,它似乎不起作用.我这样使用它:
if [ ! checkProcess ]; then
#do something, like rm file containing PID
fi
Run Code Online (Sandbox Code Playgroud)
但是如果我只是使用下面的实现它可以正常工作:
if [ ! kill -s 0 $(getPid) > /dev/null 2>&1 ]; then
#do something, like rm file containing PID
fi
Run Code Online (Sandbox Code Playgroud)
是否有一个原因?我没有在函数中返回正确的值吗?还是只是错误地实施了?