kmisc 2.1.114__py3-none-any.whl → 2.1.116__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kmisc/__init__.py CHANGED
@@ -34,7 +34,10 @@ from http.cookies import Morsel # This module for requests when you use build by
34
34
  import xml.etree.ElementTree as ET
35
35
  from email.mime.text import MIMEText
36
36
  from email.mime.base import MIMEBase
37
- from multiprocessing import Process, Queue
37
+ import multiprocessing
38
+ Process=multiprocessing.Process
39
+ Queue=multiprocessing.Queue
40
+ #from multiprocessing import Process, Queue
38
41
  from email.mime.multipart import MIMEMultipart
39
42
  try:
40
43
  from kmport import *
@@ -64,6 +67,7 @@ pipe_file=None
64
67
  log_new_line='\n'
65
68
  url_group = re.compile('^(https|http|ftp)://([^/\r\n]+)(/[^\r\n]*)?')
66
69
  cdrom_ko=['sr_mod','cdrom','libata','ata_piix','ata_generic','usb-storage']
70
+ env_kmp=Environment(name='__KmP__')
67
71
 
68
72
  def Abs(*inps,**opts):
69
73
  default=opts.get('default',None)
@@ -2734,35 +2738,119 @@ def net_start_single_server(server_port,main_func_name,server_ip='',timeout=0,ma
2734
2738
  ssoc.close()
2735
2739
  return rc
2736
2740
 
2737
- def kmp(mp={},func=None,name=None,timeout=0,quit=False,log_file=None,log_screen=True,log_raw=False, argv=[],queue=None):
2741
+ def kmp(mp={},func=None,name=None,timeout=0,quit=False,log_file=None,log_screen=True,log_raw=False, argv=[],queue=False):
2742
+ #Log
2743
+ def LLOG(msg):
2744
+ if 'log' in mp and 'queue' in mp['log']:
2745
+ mp['log']['queue'].put(msg)
2746
+ time.sleep(2)
2747
+
2748
+ # clean thread
2749
+ def kill_thread(thread):
2750
+ if not thread.is_alive():
2751
+ return
2752
+ tid = ctypes.c_long(thread.ident)
2753
+ res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(SystemExit))
2754
+ if res > 1:
2755
+ ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
2756
+
2738
2757
  # Clean
2758
+ def terminate_process(pobj,max_retry=3):
2759
+ def kill_process(pobj,max_retry):
2760
+ for i in range(max_retry):
2761
+ if isinstance(pobj,dict) and 'mp' in pobj:
2762
+ pobj=pobj['mp']
2763
+ if type(pobj).__name__ == 'Process':
2764
+ try:
2765
+ if pobj.is_alive():
2766
+ pobj.terminate()
2767
+ pobj.join()
2768
+ else:
2769
+ return True
2770
+ except:
2771
+ pass
2772
+ time.sleep(3)
2773
+ else:
2774
+ break
2775
+ return False
2776
+ if isinstance(pobj,dict):
2777
+ return kill_process(pobj,max_retry)
2778
+ elif isinstance(pobj,list):
2779
+ rc=True
2780
+ for pp in pobj:
2781
+ if not kill_process(pp,max_retry):
2782
+ rc=False
2783
+ return rc
2784
+
2739
2785
  for n in [k for k in mp]:
2740
- if quit is True:
2741
- if n != 'log':
2742
- mp[n]['mp'].terminate()
2743
- if 'log' in mp:
2744
- mp['log']['queue'].put('\nterminate function {}'.format(n))
2745
- else:
2746
- if mp[n]['timeout'] > 0 and TIME().Int() > mp[n]['timeout']:
2747
- mp[n]['mp'].terminate()
2748
- if 'log' in mp:
2749
- mp['log']['queue'].put('\ntimeout function {}'.format(n))
2750
- if not mp[n]['mp'].is_alive():
2786
+ if isinstance(mp[k],dict):
2787
+ timeout=mp[n].get('timeout',0)
2788
+ if quit is True or timeout > 0 and TIME().Int() > timeout:
2789
+ if n != 'log':
2790
+ if 'mp' in mp[n]:
2791
+ LLOG('\nterminate function {}'.format(n))
2792
+ if terminate_process(mp[n]):
2793
+ del mp[n]
2794
+ elif isinstance(mp[k],list):
2795
+ for mm in mp[n]:
2796
+ if not isinstance(mm,dict): continue
2797
+ timeout=mm.get('timeout',0)
2798
+ if quit is True or timeout > 0 and TIME().Int() > timeout:
2799
+ if n != 'log':
2800
+ if 'mp' in mm:
2801
+ LLOG('\nterminate function {}'.format(n))
2802
+ terminate_process(mm)
2751
2803
  del mp[n]
2752
- if quit is True and 'log' in mp:
2753
- mp['log']['queue'].put('\nterminate function log')
2754
- TIME().Sleep(2)
2755
- mp['log']['mp'].terminate()
2804
+ env_mp=env_kmp.get()
2805
+ for n in [k for k in env_mp]:
2806
+ if isinstance(env_mp[n],dict):
2807
+ timeout=env_mp[n].get('timeout',0)
2808
+ if quit is True or timeout > 0 and TIME().Int() > timeout:
2809
+ if n != 'log':
2810
+ if 'mp' in env_mp[n]:
2811
+ LLOG('\nterminate function {}'.format(n))
2812
+ if terminate_process(env_mp[n]):
2813
+ env_kmp.remove(n)
2814
+ elif isinstance(env_mp[n],list):
2815
+ for mm in env_mp[n]:
2816
+ if not isinstance(mm,dict): continue
2817
+ timeout=mm.get('timeout',0)
2818
+ if quit is True or timeout > 0 and TIME().Int() > timeout:
2819
+ if n != 'log':
2820
+ if 'mp' in mm:
2821
+ LLOG('\nterminate function {}'.format(n))
2822
+ terminate_process(mm)
2823
+ env_kmp.remove(n)
2824
+ if quit is True:
2825
+ #cleanup log
2826
+ if 'log' in mp:
2827
+ mp['log']['stop']=True # stop signal
2828
+ if 'queue' in mp['log']:
2829
+ mp['log']['queue'].put('__exit__all__') #stop message
2830
+ if 'mp' in mp['log']:
2831
+ if terminate_process(mp['log']['mp']): # terminate
2832
+ del mp['log']
2833
+ #cleanup all child process again
2834
+ for c in multiprocessing.active_children():
2835
+ c.terminate()
2836
+ c.join()
2837
+ #kill still remained
2838
+ for c in multiprocessing.active_children():
2839
+ c.kill()
2840
+ c.join()
2756
2841
  return
2757
2842
 
2758
2843
  # LOG
2759
- def logging(ql,log_file=None,log_screen=True,raw=False):
2844
+ def logging(ql,log_file=None,log_screen=False,raw=False,mp={}):
2760
2845
  while True:
2761
- #if not ql.empty():
2846
+ if isinstance(mp,dict) and mp.get('log',{}).get('stop'): # if stop signal then break
2847
+ break
2762
2848
  if ql.empty():
2763
2849
  TIME().Sleep(0.01)
2764
2850
  else:
2765
2851
  ll=ql.get()
2852
+ if ll == '__exit__all__':
2853
+ break # if message are exit message then quit
2766
2854
  if raw:
2767
2855
  log_msg=ll
2768
2856
  else:
@@ -2776,14 +2864,17 @@ def kmp(mp={},func=None,name=None,timeout=0,quit=False,log_file=None,log_screen=
2776
2864
  sys.stdout.write(log_msg)
2777
2865
  sys.stdout.flush()
2778
2866
 
2779
- if 'log' not in mp or not mp['log']['mp'].is_alive():
2780
- #log=multiprocessing.Queue()
2781
- log=Queue()
2782
- #lqp=multiprocessing.Process(name='log',target=logging,args=(log,log_file,log_screen,log_raw,))
2783
- lqp=Process(name='log',target=logging,args=(log,log_file,log_screen,log_raw,))
2784
- lqp.daemon = True
2785
- mp.update({'log':{'mp':lqp,'start':TIME().Int(),'timeout':0,'queue':log}})
2786
- lqp.start()
2867
+ #Start Log Daemon
2868
+ if log_screen or log_file:
2869
+ if 'log' not in mp:
2870
+ log=Queue()
2871
+ lqp=Process(name='log',target=logging,args=(log,log_file,log_screen,log_raw,mp,))
2872
+ lqp.daemon = True
2873
+ mp['log']={'mp':lqp,'start':TIME().Int(),'timeout':0,'queue':log}
2874
+ lqp.start()
2875
+ else:
2876
+ if not mp['log']['mp'].is_alive():
2877
+ mp['log']['mp'].start()
2787
2878
 
2788
2879
  # Functions
2789
2880
  if func:
@@ -2791,22 +2882,18 @@ def kmp(mp={},func=None,name=None,timeout=0,quit=False,log_file=None,log_screen=
2791
2882
  name=func.__name__
2792
2883
  if name not in mp:
2793
2884
  if argv:
2794
- #mf=multiprocessing.Process(name=name,target=func,args=tuple(argv))
2795
2885
  mf=Process(name=name,target=func,args=tuple(argv))
2796
2886
  else:
2797
- #mf=multiprocessing.Process(name=name,target=func)
2798
2887
  mf=Process(name=name,target=func)
2799
2888
  if timeout > 0:
2800
2889
  timeout=TIME().Int()+timeout
2801
-
2802
- # for aa in argv:
2803
- # if type(aa).__name__ == 'Queue':
2804
- # mp.update({name:{'mp':mf,'timeout':timeout,'start':now(),'queue':aa}})
2805
- if name not in mp:
2890
+ if name in mp:
2891
+ printf('Already multiprocess has same name',logfile=log_file,mode='d')
2892
+ else:
2806
2893
  if queue and type(queue).__name__ == 'Queue':
2807
- mp.update({name:{'mp':mf,'timeout':timeout,'start':TIME().Int(),'queue':queue}})
2894
+ mp[name]={'mp':mf,'timeout':timeout,'start':TIME().Int(),'queue':queue}
2808
2895
  else:
2809
- mp.update({name:{'mp':mf,'timeout':timeout,'start':TIME().Int()}})
2896
+ mp[name]={'mp':mf,'timeout':timeout,'start':TIME().Int()}
2810
2897
  mf.start()
2811
2898
  return mp
2812
2899
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.114
3
+ Version: 2.1.116
4
4
  Summary: Enginering useful library
5
5
  Home-page: https://github.com/kagepark/kmisc
6
6
  Author: Kage Park
@@ -0,0 +1,6 @@
1
+ kmisc/__init__.py,sha256=-z35GjgAzX_LWAy8tm6p3KHixrHDGEcdOLEdW7AzxTQ,141127
2
+ kmisc-2.1.116.dist-info/LICENSE,sha256=mn9ekhb34HJxsrVhcxrLXJUzy55T62zg-Gh9Ro0mVJI,1066
3
+ kmisc-2.1.116.dist-info/METADATA,sha256=3QeoLeH-ktOpycmC1c1nYxysTa201drbax0MqCG0Bw8,5523
4
+ kmisc-2.1.116.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
5
+ kmisc-2.1.116.dist-info/top_level.txt,sha256=wvdHf5aQTqcGYvxk-F9E_BMWLMhlwC8INBmwO-V6_X4,6
6
+ kmisc-2.1.116.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- kmisc/__init__.py,sha256=1b2psgkwI69O6mgGuIq6ftoWsKjWGyWxXxELwza4a5I,137942
2
- kmisc-2.1.114.dist-info/LICENSE,sha256=mn9ekhb34HJxsrVhcxrLXJUzy55T62zg-Gh9Ro0mVJI,1066
3
- kmisc-2.1.114.dist-info/METADATA,sha256=IAfRdDGTsTSJ5dx1UsMDoxKgs0KgjPpzfhuCTJfbHe8,5523
4
- kmisc-2.1.114.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
5
- kmisc-2.1.114.dist-info/top_level.txt,sha256=wvdHf5aQTqcGYvxk-F9E_BMWLMhlwC8INBmwO-V6_X4,6
6
- kmisc-2.1.114.dist-info/RECORD,,