def show_tree(sexpr, format): tree = parse_sexpr(sexpr) tuples = graph_tuples(tree) dotstr = dot_from_tuples(tuples) filename = "/tmp/parseviz.%s.%s.%s" % (os.getid(), time.time(),format) call_dot(dotstr, filename, format=format) return filename
def Counter(name, mutex, block, word_map_list): print('Run child process %s %s.....' % (name, os.getid())) for line in block: line = line.split(' ') for w in line: if w: if w not in word_map: word_map[w] = 0 word_map[w] += 1 #mutex.acquire() #mutex.release() print("work_%d finished size = %d\n" % (name, len(word_map))) return word_map
header = struct.pack("bbHHh",ICMP_ECHO_REQUEST,0,socket.htons(my_checksum),ID,1) packet = header + data sock.sendto(packet,(target_addr,1)) def ping_once(self): icmp = socket.getprotobyname("imcp") try: sock = socket.socket(socket.AF_INET,socket.SOCK_RAW,icmp) except socket.error,(error,msg): if errno == 1: msg += "ICMP messages can only be sent from root user processes" raise socket.error(msg) except Exception,e: print "Exception : %s" %(e) my_ID = os.getid() & OxFFFF self.send_ping(sock,my_ID) delay = self.receive_pong(sock,my_ID,self.timeout) sock.close() return delay def ping(self): for i in xrange(self.count): print "Ping to $s..." % self.target_host, try: delay = self.ping_once() except socket.gaierror,e: print "PIng failed.(socket error: '%s')" % e[1] break if delay == None:
def pool_proc(name): print('running proc (%s)in Pool' %os.getid()) start = time.time() time.sleep(random.random()*2) end = time.time() print('Task %s cost time: %s' %(name, (end - start)))
#!/usr/bin/env python # -*- coding: utf-8 -*- import os print('Process (%s) start...' % os.getpid()) #only work on Unix/linux/Mac pid = os.fork() if pid == 0: print("I am child process (%s) and my parent is %s." % s(os.getid(), os.getppid())) else: print("I (%s) just created a child process (%s)." % (os.getpid(), pid))
def run(self): #Opening the JSON config file containing the device and sensor details with open('/home/pi/python3_venv/AWS/config1.txt') as specs: data = json.load( specs, object_pairs_hook=OrderedDict ) #storing the contents of JSON config file keeping the order intact # print("^^^^^^^^^^^^^^^^change^^^^^^^^^^^^^",data["Specs"]["analog_input"]["Boiler2"]["sensors"]["sensor1"]["sensor_id"]) specs = data["Specs"] #Looping through the JSON config file contents for con in specs: #Checking for all the devices under modbus RTU and storing the details in different arrays if con == "modbus_rtu": ports = [] serial_no = [] parity = [] data_bits = [] stop_bits = [] baud_rate = [] j = 0 for p in data["Specs"]["modbus_rtu"]: ports.append(p) #Iterating through each port and storing the details for p in ports: dev = [] serial_no.append( data["Specs"]["modbus_rtu"][p]["serial_no"]) baud_rate.append( data["Specs"]["modbus_rtu"][p]["baud_rate"]) parity.append(data["Specs"]["modbus_rtu"][p]["parity"]) data_bits.append( data["Specs"]["modbus_rtu"][p]["data_bits"]) stop_bits.append( data["Specs"]["modbus_rtu"][p]["stop_bits"]) for d in data["Specs"]["modbus_rtu"][p]["Devices"]: dev.append(d) dev_no = [] dev_id = [] json_rgstr = [] boilers_rtu = [] #Iterating through all the devices in each port for d in dev: dev_no.append(data["Specs"]["modbus_rtu"][p] ["Devices"][d]["device_no"]) dev_id.append(data["Specs"]["modbus_rtu"][p] ["Devices"][d]["device_id"]) boilers_rtu.append(d) json_rgstr.append(data["Specs"]["modbus_rtu"][p] ["Devices"][d]["registers"]) try: process1 = Process( target=self.registers_rtu, args=(serial_no[j], baud_rate[j], parity[j], data_bits[j], stop_bits[j], dev_no, dev_id, json_rgstr)) time.sleep(.2) process1.start() except KeyboardInterrupt: process1.terminate() print( "Process {} terminated by KeyboardInterrupt" .format(os.getid())) #Creating threads for each port j = j + 1 #Checking for all the devices under modbus tcp and storing the details in different arrays if con == "modbus_tcp": boilers_tcp = [] ip = [] port = [] dev_id = [] u = [] json_rgstr = [] for b in data["Specs"]["modbus_tcp"]: ip.append(data["Specs"]["modbus_tcp"][b]["ip_address"]) port.append(data["Specs"]["modbus_tcp"][b]["port"]) dev_id.append( data["Specs"]["modbus_tcp"][b]["device_id"]) u.append( data["Specs"]["modbus_tcp"][b]["unit_address"]) boilers_tcp.append(b) #storing the details of each register for all the devices/boilers under tcp in an array for i in range(len(boilers_tcp)): json_rgstr.append(data["Specs"]["modbus_tcp"][ boilers_tcp[i]]["registers"]) #array to create multiple threads for each device/ boiler data under tcp(number of threads = number of boilers) for i in range(len(json_rgstr)): try: process2 = Process(target=self.registers_tcp, args=(json_rgstr[i], ip[i], port[i], u[i])) time.sleep(.2) process2.start() except KeyboardInterrupt: process2.terminate() print("Process {} terminated by KeyboardInterrupt". format(os.getid())) #storing the details of each device/boiler under analog input in an array and passing them to the "sensors_ai" function if con == "analog_input": boilers_ai = [] for b in data["Specs"]["analog_input"]: boilers_ai.append(b) dev_id.append( data["Specs"]["analog_input"][b]["device_id"]) json_sensors = [] #storing the sensor details in JSON format in the variable "json_sensors" for i in range(len(boilers_ai)): json_sensors.append(data["Specs"]["analog_input"][ boilers_ai[i]]["sensors"]) #array to create multiple threads for each device/ boiler data under analog input(number of threads = number of boilers) for i in range(len(json_sensors)): try: process3 = Process(target=self.sensors_ai, args=(json_sensors[i], dev_id[i])) time.sleep(.2) process3.start() except KeyboardInterrupt: process3.terminate() print("Process {} terminated by KeyboardInterrupt". format(os.getid()))
def pid_cpuset(self, pid=None): if pid is None: pid = os.getid() return self.groups_cpuset(self.pid_groups(pid))
def pid_close(self, pid): if pid is None: pid = os.getid() self.groups_close(self.pid_groups(pid))
def pid_stats(self, pid=None): if pid is None: pid = os.getid() return self.groups_stats(self.pid_groups(pid))
import os import time ret = os.fork() print(ret) if ret == 0: while True: print(“-------ppprogress------”) print("%d"%os.getid()) time.sleep(1) else: while True: print("-------pprogress------") print("%d"%os.getppid()) time.sleep(1)
def long_time_task(name): print("Run task %s (%s)..." % (name, os.getid())) start = time.time() time.sleep(random.random() * 3) end = time.time() print("Task %s runs %0.2f seconds." % (name, (end - start)))
# -*-coding:utf-8-*- from multiprocessing import Pool # 进程池方式创建进程 import os, time, random def long_time_task(name): print("Run task %s (%s)..." % (name, os.getid())) start = time.time() time.sleep(random.random() * 3) end = time.time() print("Task %s runs %0.2f seconds." % (name, (end - start))) if __name__ == "__main__": print("Parent process %s." % os.getid()) p = Pool() # p = Pool(5)同时进行5个进程 for i in range(5): p.apply_async(long_time_task, args=(i, )) # 批量创建进程 print("Waiting for all subprocesses done") p.close() p.join() print("All subprocesses done")