这是我要执行的示例代码:
public void SaveImage(int resId){
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resId);
try {
OutputStream fOut = null;
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Folder", getResources().getResourceEntryName(resId)+".PNG");
file.createNewFile();
fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (Exception e) {
}
}
我有两个类,第一个是class myfiles:
def __init__:
self.input_file = ""
self.output_file = ""
class process_files:
def __init__(self, list_of_myfiles):
self.myfiles_list = list_of_myfiles
self.my_new_files = []
def intial_work(self):
list_of_objects = []
for f in self.myfiles_list:
new_files = myfiles()
new_files.input_file = f
list_of_objects.append(new_files)
self.my_new_files = list_of_objects
def update_files(self):
# this will update the instance output_file
for f in self.my_new_files:
# do some work on input file and write it to ouput new_files
process_files.work_function(f)
# this will not update the instance output_file
n_cores = psutil.cpu_count()
p = multiprocessing.Pool(n_cores)
p.map(process_files.work_function, self.my_new_files)
@staticmethod
def work_function(new_file):
# do some work on input file and update output
new_file.output_file = "something new"
,我将把它用作对象列表。
第二个是myfiles
,它将处理上一个类中的对象。
process_files
在方法list_of_files = ["a", "b", "c"]
myprocess = process_files(list_of_files)
myprocess.intial_work()
myprocess.update_files()
中,如果我使用循环来更新实例update_files()
将具有一个值,但是如果我使用myfiles.output_file
将始终为空字符串,为什么?
如果Pool将不起作用。还有另一种方式可以并行运行它们吗? (多线程或多处理)