Python多重处理:在并行执行之前和之后串行执行代码

时间:2018-08-05 23:37:56

标签: python multiprocessing python-multiprocessing pool

这里的新手:我正在尝试依次执行一些代码,然后创建线程池并并行执行一些代码。并行执行完成后,我想串行执行更多代码。

例如...

import time
from multiprocessing import Pool

print("I only want to print this statement once")



def worker(i):
    """worker function"""
    now = time.time()
    time.sleep(i)
    then = time.time()
    print(now, then)

if __name__ == '__main__':
    with Pool(3) as p:
        p.map(worker, [1, 1, 1])
        p.close()

print("Only print this once as well")

我想返回...

I only want to print this statement once
1533511478.0619314 1533511479.0620182
1533511478.0789354 1533511479.0791905
1533511478.0979397 1533511479.098235
Only print this once as well

但是它返回的是:

I only want to print this statement once
I only want to print this statement once
Only print this once as well
I only want to print this statement once
Only print this once as well
I only want to print this statement once
Only print this once as well
I only want to print this statement once
Only print this once as well
I only want to print this statement once
Only print this once as well
1533511478.0619314 1533511479.0620182
1533511478.0789354 1533511479.0791905
1533511478.0979397 1533511479.098235
Only print this once as well

因此,似乎每个池都要额外运行一次打印语句。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

基于观察到的行为,我假设您使用的是NT / Windows操作系统。

看到所有这些打印件的原因是因为在Windows上使用了spawn启动策略。当“产生”新进程时,将启动一个新的Python解释器,它会接收应该执行的模块和功能。新解释器导入模块时,将执行顶层__main__函数。因此,重复打印。

只需在class YourViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CustomCellDelegate { @IBOutlet weak var tableView: UITableView? override func viewDidLoad() { super.viewDidLoad() } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 2 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "identifier") as? CustomCell else { return UITableViewCell() } cell.indexPath = indexPath cell.delegate = self return cell } func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { return nil } func delete(for cell: CustomCell) { let indexPath = cell.indexPath print("doing action for cell at: \(indexPath!.row)") // your implementation for action // maybe delete a cell or whatever? cell.hideDeleteActions() } } protocol CustomCellDelegate: class { func delete(for cell: CustomCell) } class CustomCell: UITableViewCell { weak var delegate: CustomCellDelegate? var indexPath: IndexPath! @IBOutlet weak var buttonAction: UIButton? @IBOutlet weak var constBtnDeleteAction: NSLayoutConstraint? override func awakeFromNib() { self.constBtnDeleteAction?.constant = 0 } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { guard let point = touches.first?.location(in: self) else { return } if self.point(inside: point, with: event) { self.showDeleteActions() } } func showDeleteActions() { self.constBtnDeleteAction?.constant = 100 // Set it according to the button width UIView.animate(withDuration: 0.3) { self.layoutIfNeeded() } } func hideDeleteActions() { self.constBtnDeleteAction?.constant = 0 UIView.animate(withDuration: 0.3) { self.layoutIfNeeded() } } @IBAction func cellButtonAction() { delegate?.delete(for: self) } } 中移动这些打印语句,您就不会再看到它们。