2013-05-16 00:14:38 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#-*- coding: utf-8 -*-
|
2013-05-06 02:16:30 +00:00
|
|
|
# psr/mproc.py
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
2013-04-20 11:36:49 +00:00
|
|
|
#
|
2013-05-06 02:16:30 +00:00
|
|
|
# software : Kirmah <http://kirmah.sourceforge.net/>
|
2013-05-15 18:51:08 +00:00
|
|
|
# version : 2.18
|
2014-07-17 22:19:51 +00:00
|
|
|
# date : 2014
|
2013-05-06 02:16:30 +00:00
|
|
|
# licence : GPLv3.0 <http://www.gnu.org/licenses/>
|
|
|
|
# author : a-Sansara <[a-sansara]at[clochardprod]dot[net]>
|
|
|
|
# copyright : pluie.org <http://www.pluie.org/>
|
2013-04-20 11:36:49 +00:00
|
|
|
#
|
2013-05-06 02:16:30 +00:00
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
|
2013-04-20 11:36:49 +00:00
|
|
|
#
|
2013-05-06 02:16:30 +00:00
|
|
|
# This file is part of Kirmah.
|
|
|
|
#
|
|
|
|
# Kirmah is free software (free as in speech) : you can redistribute it
|
|
|
|
# and/or modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
# or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Kirmah is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
# more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Kirmah. If not, see <http://www.gnu.org/licenses/>.
|
2013-04-20 11:36:49 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~ module mproc ~~
|
|
|
|
|
2013-05-07 00:43:02 +00:00
|
|
|
from multiprocessing import Process, current_process, Pipe
|
2013-05-06 02:16:30 +00:00
|
|
|
from multiprocessing.connection import wait
|
|
|
|
from threading import current_thread
|
|
|
|
from psr.sys import Sys, Const, init
|
|
|
|
from psr.log import Log
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# ~~ class Worker ~~
|
|
|
|
|
|
|
|
class Worker:
|
|
|
|
|
|
|
|
@Log(Const.LOG_BUILD)
|
2013-05-07 00:43:02 +00:00
|
|
|
def __init__(self, appname, debug, gui, color, loglvl, ppid, event, id, wp, delay, task, *args, **kwargs):
|
2013-05-06 02:16:30 +00:00
|
|
|
def mptask(id, *args, **kwargs):
|
|
|
|
Sys.sendMainProcMsg(Manager.MSG_INIT, None)
|
2013-05-07 00:43:02 +00:00
|
|
|
otask = task(id=id, event=event, *args, **kwargs)
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.sendMainProcMsg(Manager.MSG_END, None)
|
|
|
|
return otask
|
|
|
|
|
|
|
|
init(appname, debug, ppid, color, loglvl)
|
2013-05-07 00:43:02 +00:00
|
|
|
Sys.g.WPIPE = wp
|
|
|
|
Sys.g.CPID = id
|
|
|
|
Sys.g.GUI = gui
|
|
|
|
# initialize child process event with parent process event
|
|
|
|
Sys.g.MPEVENT = event
|
2013-05-06 02:16:30 +00:00
|
|
|
if delay : Sys.sleep(delay)
|
|
|
|
mptask(id, *args, **kwargs)
|
2013-05-07 00:43:02 +00:00
|
|
|
# don't directly close pipe 'cause of eventual logging
|
2013-05-06 02:16:30 +00:00
|
|
|
# pipe will auto close on terminating child process
|
2013-04-20 11:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2013-05-06 02:16:30 +00:00
|
|
|
# ~~ class Manager ~~
|
2013-04-20 11:36:49 +00:00
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
class Manager:
|
|
|
|
|
|
|
|
MSG_INIT = 0
|
|
|
|
MSG_PRINT = 1
|
|
|
|
MSG_DATA = 2
|
|
|
|
MSG_END = 3
|
|
|
|
TYPE_MSG = list(range(4))
|
|
|
|
K_ID = 0
|
|
|
|
K_TYPE = 1
|
|
|
|
K_DATA = 2
|
|
|
|
K_PROC = 0
|
|
|
|
K_PIPE = 1
|
|
|
|
|
|
|
|
checktime = None
|
|
|
|
|
|
|
|
@Log(Const.LOG_UI)
|
2013-05-07 00:43:02 +00:00
|
|
|
def __init__(self, task, nproc=2, delay=None, event=None, *args, **kwargs):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
self.readers = []
|
|
|
|
self.plist = []
|
|
|
|
self.onstart_bind = None
|
|
|
|
self.onrun_bind = None
|
|
|
|
self.onend_bind = None
|
|
|
|
for id in range(nproc):
|
|
|
|
r, w = Pipe(duplex=False)
|
|
|
|
self.readers.append(r)
|
|
|
|
# (process, wpipe)
|
2013-05-07 00:43:02 +00:00
|
|
|
p = Process(target=Worker, args=tuple([Sys.g.PRJ_NAME, Sys.g.DEBUG, Sys.g.GUI, Sys.g.COLOR_MODE, Sys.g.LOG_LEVEL, Sys.getpid(), event, id, w, delay, task])+tuple(args), kwargs=kwargs)
|
2013-05-06 02:16:30 +00:00
|
|
|
self.plist.append((p, w))
|
|
|
|
|
|
|
|
@Log(Const.LOG_APP)
|
|
|
|
def run(self, checktime=None, onstart_bind=None, onrun_bind=None, onend_bind=None):
|
|
|
|
self.checktime = checktime
|
|
|
|
self.onstart_bind = onstart_bind
|
|
|
|
self.onrun_bind = onrun_bind
|
|
|
|
self.onend_bind = onend_bind
|
|
|
|
for p, w in self.plist:
|
|
|
|
p.start()
|
|
|
|
w.close()
|
|
|
|
self.wait()
|
2013-04-20 11:36:49 +00:00
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
|
|
|
|
@Log(Const.LOG_DEBUG)
|
|
|
|
def wait(self):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
while self.readers:
|
|
|
|
self.wait_childs()
|
|
|
|
if self.checktime is not None : Sys.sleep(self.checktime)
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
def getcpid(self, id):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
return self.plist[id][self.K_PROC].pid
|
|
|
|
|
2013-04-20 11:36:49 +00:00
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
@Log(Const.LOG_ALL)
|
|
|
|
def wait_childs(self):
|
2013-04-20 11:36:49 +00:00
|
|
|
""""""
|
2013-05-06 02:16:30 +00:00
|
|
|
for r in wait(self.readers):
|
|
|
|
try:
|
|
|
|
msg = r.recv()
|
|
|
|
except EOFError:
|
|
|
|
self.readers.remove(r)
|
|
|
|
else:
|
|
|
|
if len(msg)==3 and msg[self.K_TYPE] in self.TYPE_MSG :
|
|
|
|
|
|
|
|
cpid = self.getcpid(msg[self.K_ID])
|
|
|
|
|
|
|
|
if msg[self.K_TYPE] == self.MSG_INIT :
|
|
|
|
if hasattr(self.onstart_bind, '__call__'):
|
|
|
|
self.onstart_bind(msg[self.K_ID], cpid, msg[self.K_DATA])
|
|
|
|
|
|
|
|
elif msg[self.K_TYPE] == self.MSG_PRINT :
|
|
|
|
if Sys.g.DEBUG :
|
|
|
|
if not Sys.g.GUI :
|
|
|
|
for item in msg[self.K_DATA] :
|
2014-07-16 23:55:55 +00:00
|
|
|
Sys.echo(item[0], Sys.clzdic[item[1]], False, True)
|
2013-05-06 02:16:30 +00:00
|
|
|
Sys.dprint('')
|
|
|
|
#~ else :
|
|
|
|
Sys.wlog(msg[self.K_DATA])
|
2013-04-20 11:36:49 +00:00
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
elif msg[self.K_TYPE] == self.MSG_DATA :
|
|
|
|
if hasattr(self.onrun_bind, '__call__'):
|
|
|
|
self.onrun_bind(msg[self.K_ID], cpid, msg[self.K_DATA])
|
2013-04-20 11:36:49 +00:00
|
|
|
|
2013-05-06 02:16:30 +00:00
|
|
|
elif msg[self.K_TYPE] == self.MSG_END :
|
|
|
|
if hasattr(self.onend_bind, '__call__'):
|
|
|
|
self.onend_bind(msg[self.K_ID], cpid, msg[self.K_DATA])
|