python进程暂停_如何在Python中暂停多处理进程?

I want the user to be able to pause an execution of the multiprocessing at any given time after the was started.How to achieve it?

my code is :

# -*- coding: utf-8 -*-

from PySide import QtCore, QtGui

from Ui_MainWindow import Ui_MainWindow

from queue import Queue

import sys

import multiprocessing, os, time

def do_work():

print ('Work Started: %d' % os.getpid())

time.sleep(1)

return 'Success'

def manual_function(job_queue, result_queue):

while not job_queue.empty():

try:

job = job_queue.get(block=False)

result_queue.put(do_work())

except Queue.Empty:

pass

class Worker(QtCore.QThread):

def __init__(self,name):

QtCore.QThread.__init__(self)

self.name = name

self.pause = False

def run(self):

job_queue = multiprocessing.Queue()

result_queue = multiprocessing.Queue()

for i in range(1000):

job_queue.put(None)

self.workers = []

for i in range(6):

tmp = multiprocessing.Process(target=manual_function, args=(job_queue, result_queue))

tmp.start()

self.workers.append(tmp)

def paused(self):

'''

pause / resumme ?????????????????????????

'''

def stop(self):

for worker in self.workers:

print ("end")

worker.terminate()

worker.join()

class MainWindow(QtGui.QMainWindow):

def __init__(self, parent=None):

QtGui.QMainWindow.__init__(self, parent)

self.ui = Ui_MainWindow()

self.ui.setupUi(self)

self.connect(self.ui.actionStart, QtCore.SIGNAL('triggered()'),self.start)

self.connect(self.ui.actionStop, QtCore.SIGNAL('triggered()'),self.stop)

self.connect(self.ui.actionPause, QtCore.SIGNAL('triggered()'),self.pause)

def pause(self):

self.work.paused()

def stop(self):

self.work.stop()

def start(self):

self.threads = []

for tName in range(1):

self.work = Worker("Thread-%s"%tName)

self.threads.append(self.work)

self.work.start()

if __name__ == "__main__":

app = QtGui.QApplication (sys.argv)

window = MainWindow()

window.show()

sys.exit(app.exec_())

How to pause button click Multiprocessing Process in Python?

解决方案

Use a simple flag as shared memory which determines if the spawned process runs or not. Check out a little example.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

from multiprocessing import Process, Value

from time import sleep

def do_work():

print("Working...")

sleep(1)

def worker(run):

while True:

if run.value:

do_work()

if __name__ == "__main__":

run = Value("i", 1)

p = Process(target=worker, args=(run,))

p.start()

while True:

raw_input()

run.value = not run.value

I share an instance of Value between processes. Hit Enter to pause or resume the spawned process.

This works in Python 2, you should indicate which version are you running.

Hope it helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值