kmisc 2.1.115__tar.gz → 2.1.116__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.115
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
@@ -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,62 +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):
2738
2742
  #Log
2739
2743
  def LLOG(msg):
2740
2744
  if 'log' in mp and 'queue' in mp['log']:
2741
2745
  mp['log']['queue'].put(msg)
2742
2746
  time.sleep(2)
2743
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
+
2744
2757
  # Clean
2745
- def terminate_process(pobj,max_retry=5):
2746
- for i in range(max_retry):
2747
- if isinstance(pobj,dict) and 'mp' in pobj:
2748
- pobj=pobj['mp']
2749
- if type(pobj).__name__ == 'Process':
2750
- try:
2751
- if pobj.is_alive():
2752
- pobj.terminate()
2753
- return True
2754
- except:
2755
- time.sleep(1)
2756
- else:
2757
- break
2758
- return False
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
2759
2784
 
2760
2785
  for n in [k for k in mp]:
2761
- if quit is True:
2762
- if n != 'log':
2763
- if 'mp' in mp[n] and mp[n]['mp']:
2764
- LLOG('\nterminate function {}'.format(n))
2765
- if terminate_process(mp[n]):
2766
- del mp[n]
2767
- else:
2768
- if mp[n]['timeout'] > 0 and TIME().Int() > mp[n]['timeout']:
2769
- if 'mp' in mp[n]:
2770
- if terminate_process(mp[n]['mp']):
2771
- LLOG('\ntimeout function {}'.format(n))
2772
- del mp[n]['mp']
2773
- if quit is True and 'log' in mp:
2774
- LLOG('\nterminate function log')
2775
- if 'mp' in mp['log']:
2776
- if terminate_process(mp['log']['mp']):
2777
- del mp['log']['mp']
2778
- if 'queue' in mp['log']:
2779
- if terminate_process(mp['log']['queue']):
2780
- del mp['log']['queue']
2781
- if 'mp' not in mp['log'] and 'queue' not in mp['log']:
2782
- del mp['log']
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)
2803
+ del mp[n]
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()
2783
2841
  return
2784
2842
 
2785
2843
  # LOG
2786
- def logging(ql,log_file=None,log_screen=True,raw=False):
2844
+ def logging(ql,log_file=None,log_screen=False,raw=False,mp={}):
2787
2845
  while True:
2788
- #if not ql.empty():
2846
+ if isinstance(mp,dict) and mp.get('log',{}).get('stop'): # if stop signal then break
2847
+ break
2789
2848
  if ql.empty():
2790
2849
  TIME().Sleep(0.01)
2791
2850
  else:
2792
2851
  ll=ql.get()
2852
+ if ll == '__exit__all__':
2853
+ break # if message are exit message then quit
2793
2854
  if raw:
2794
2855
  log_msg=ll
2795
2856
  else:
@@ -2803,14 +2864,17 @@ def kmp(mp={},func=None,name=None,timeout=0,quit=False,log_file=None,log_screen=
2803
2864
  sys.stdout.write(log_msg)
2804
2865
  sys.stdout.flush()
2805
2866
 
2806
- if 'log' not in mp or not mp['log']['mp'].is_alive():
2807
- #log=multiprocessing.Queue()
2808
- log=Queue()
2809
- #lqp=multiprocessing.Process(name='log',target=logging,args=(log,log_file,log_screen,log_raw,))
2810
- lqp=Process(name='log',target=logging,args=(log,log_file,log_screen,log_raw,))
2811
- lqp.daemon = True
2812
- mp.update({'log':{'mp':lqp,'start':TIME().Int(),'timeout':0,'queue':log}})
2813
- 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()
2814
2878
 
2815
2879
  # Functions
2816
2880
  if func:
@@ -2818,22 +2882,18 @@ def kmp(mp={},func=None,name=None,timeout=0,quit=False,log_file=None,log_screen=
2818
2882
  name=func.__name__
2819
2883
  if name not in mp:
2820
2884
  if argv:
2821
- #mf=multiprocessing.Process(name=name,target=func,args=tuple(argv))
2822
2885
  mf=Process(name=name,target=func,args=tuple(argv))
2823
2886
  else:
2824
- #mf=multiprocessing.Process(name=name,target=func)
2825
2887
  mf=Process(name=name,target=func)
2826
2888
  if timeout > 0:
2827
2889
  timeout=TIME().Int()+timeout
2828
-
2829
- # for aa in argv:
2830
- # if type(aa).__name__ == 'Queue':
2831
- # mp.update({name:{'mp':mf,'timeout':timeout,'start':now(),'queue':aa}})
2832
- if name not in mp:
2890
+ if name in mp:
2891
+ printf('Already multiprocess has same name',logfile=log_file,mode='d')
2892
+ else:
2833
2893
  if queue and type(queue).__name__ == 'Queue':
2834
- mp.update({name:{'mp':mf,'timeout':timeout,'start':TIME().Int(),'queue':queue}})
2894
+ mp[name]={'mp':mf,'timeout':timeout,'start':TIME().Int(),'queue':queue}
2835
2895
  else:
2836
- mp.update({name:{'mp':mf,'timeout':timeout,'start':TIME().Int()}})
2896
+ mp[name]={'mp':mf,'timeout':timeout,'start':TIME().Int()}
2837
2897
  mf.start()
2838
2898
  return mp
2839
2899
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kmisc
3
- Version: 2.1.115
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
File without changes
File without changes
File without changes
File without changes
File without changes