Beispiel #1
0
def main():
    print('Process (%s) start...' % os.getpid())
    # Only works on Unix/Linux/Mac:
    pid = os.fork()
    if pid == 0:
        print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
    else:
        print('I (%s) just created a child process (%s).' % (os.getpid(), pid))

    print('Parent process %s.' % os.getpid())
    p = Process(target=run_proc, args=('test',))
    print('Child process will start.')
    p.start()
    p.join()
    print('Child process end.')

    print '================================'
    print('Parent process %s.' % os.getpid())
    p = Pool(4)
    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.')

    print('$ nslookup www.python.org')
    r = subprocess.call(['nslookup', 'www.python.org'])
    print('Exit code:', r)

    print('$ nslookup')
    p = subprocess.Popen(['nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, err = p.communicate(b'set q=mx\npython.org\nexit\n')
    print(output.decode('utf-8'))
    print('Exit code:', p.returncode)
class MidiClockGen:
    def __init__(self):
        self.shared_bpm = Value('i', 60)
        self.run_code = Value('i', 1)
        self.midi_process = None

    def midi_clock_generator(self, out_port, bpm, run):
        midi_output = mido.open_output(out_port)
        clock_tick = mido.Message('clock')
        while run.value:
            pulse_rate = 60.0 / (bpm.value * 24)
            midi_output.send(clock_tick)
            t1 = perf_counter()
            if bpm.value <= 3000:
                sleep(pulse_rate * 0.8)
            t2 = perf_counter()
            while (t2 - t1) < pulse_rate:
                t2 = perf_counter()

    def launch_process(self, out_port):
        self.midi_process = Process(target=self.midi_clock_generator,
                                    args=(out_port, self.shared_bpm,
                                          self.run_code))
        self.midi_process.start()

    def end_process(self):
        self.run_code = False
        self.midi_process.join()
        self.midi_process.close()
Beispiel #3
0
def main():
    crawler = Crawler()
    # restaurant_check 구하기 위한 과정
    crawler.kakao_checker('마포 진미식당')
    restaurant_list_kakao = crawler.restaurant_list_kakao
    print(restaurant_list_kakao)
    restaurant_check = input('골라라 : ')
    # 네이버 맛집 리스트 구하기
    crawler.naver_checker('마포 진미식당')
    # print(crawler.restaurant_list_naver)

    kakao_crawler = crawler.kakao_crawler(restaurant_check)
    naver_crawler = crawler.naver_crawler(restaurant_check)

    q = crawler.q

    crawlers = [kakao_crawler, naver_crawler]

    for crawler in crawlers:
        p = Process(target=crawler)
        jobs.append(p)
        p.start()

    for p in jobs:
        p.join()
        p.close()

    result = [q.get() for j in jobs]
    print(result)
Beispiel #4
0
class DataManager:
    """
    Class to interact with the Data visualizer
    @author Frederic Abraham
    """
    def __init__(self, data_names: List, pull_rate: int):
        self.manager = Manager()
        self.done = self.manager.Value("done", True)
        self.time_step = self.manager.Value("timestep", 0)
        self.line_dict = self.manager.dict(
            {data_name: 0
             for data_name in data_names})
        self.p = Process(target=run,
                         args=(
                             self.done,
                             self.time_step,
                             self.line_dict,
                         ))
        self.p.start()

    def update_time_step(self, new_time_step):
        self.time_step.value = new_time_step

    def update_value(self, key, value):
        self.line_dict[key] = value

    def stop(self):
        self.done.value = False
        self.p.join()
        self.p.close()
Beispiel #5
0
class HttpServer:
    """
    Runs a fast HTTP server for static files in another process. Seperate
    process is needed because Nimpy doesn't release the GIL.
    """
    def __init__(self, folder='.', host='localhost', port=8080, log=False, autostop=False):
        self.get_host = lambda self: host
        self.get_port = lambda self: port
        self.get_folder = lambda self: folder
        self.proc = Process(
            target=tinyhttp.serve_static_files,
            args=(host, port, folder, log)
        )
        self.autostop = autostop

    def __del__(self):
        if self.autostop: self.stop()
        
    def start(self):
        self.proc.start()

    def stop(self):
        try:
            os.kill(self.proc.pid, signal.SIGINT)
        except:
            self.proc.kill()
        finally:
            self.proc.join()
            self.proc.close()
Beispiel #6
0
class HTTPServerProcess(QObject):
    def __init__(self, handler, parent=None) -> None:
        super().__init__(parent)
        self.parent_conn, self.child_conn = Pipe()

        self.notifier = QSocketNotifier(self.parent_conn.fileno(),
                                        QSocketNotifier.Read, self)
        self.notifier.activated.connect(self.dataReady)

        self.p = Process(target=handler, args=(self.child_conn, ))

        def _on_quit():
            print("> REQUESTING HTTPD SHUTDOWN...")
            self.parent_conn.send("http_server_shutdown")
            # block current process until the child process ends correctly, then close the child
            self.p.join()
            self.p.close()

        qApp.aboutToQuit.connect(
            _on_quit)  # terminate children processes before app exit
        self.p.start()

    dataReady = PS2Signal()

    def read(self):
        return self.parent_conn.recv() if self.parent_conn.poll() else None
Beispiel #7
0
    def run(self):
        """ Run in a subprocess
        """
        def term( *args ):
            raise SystemExit()

        signal.signal(signal.SIGTERM,term)
        try:
            while True:
                # Create sub-process for handling processes creation
                # Process will seat waiting for creating contextualized processes
                p = Process(target=self.task, args=(self._queue, self._factory))
                p.start()
                p.join()
                # Test non-zero exitcode
                # This happends on Qgs provider registration failure because PyQgis exception are not 
                # propagated to python 
                if p.exitcode != 0:
                    LOGGER.critical("Sub process exited with code %s",p.exitcode)
                    self._queue.put(None)
                    break
                p.close()
        except SystemExit:
            LOGGER.debug("Factory delegate: got SystemExit()")
            if p and p.is_alive():
                p.terminate()
def test_mark_blocks_failed_when_block_computation_errs(db_factory):
    db = db_factory()

    def run_generator():
        generate_search_blocks.main(
            db_factory(),
            search_strategy=SuperabundantSearchStrategy(),
            args=FakeArgs())

    def raise_fn():
        raise Exception('Failing for a test!')

    def run_processor():
        process_search_blocks.main(
            db_factory(),
            search_strategy=make_processor_test_strategy(raise_fn))

    p1 = Process(target=run_generator)
    p1.start()
    time.sleep(5)
    p2 = Process(target=run_processor)
    p2.start()
    time.sleep(5)
    p1.terminate()
    p2.terminate()
    time.sleep(5)
    p1.close()
    p2.close()

    metadatas = db.load_metadata()
    failed_blocks = [
        x for x in metadatas if x.state == SearchBlockState.FAILED
    ]
    assert len(failed_blocks) > 0
class SocketServer(asyncore.dispatcher):
	def __init__(self, host, port):
		self.clients = Clients()
		asyncore.dispatcher.__init__(self)
		self.port = port
		self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
		self.set_reuse_addr()
		self.bind((host, port))
		self.bind = host
		self.listen(5)

	def __sock_process(self, socket):
		self.clients.append(socket)
		self.socketHandler = SocketHandler(self, socket)

	#handle when a connection is established and a connect() has been issued, add client
	def handle_accept(self):
		pair = self.accept()
		if pair != None:
			socket, addr = pair
			self.s = Process(target=self.__sock_process(socket), args=[])

			try:
				self.s.start()
			except:
				self.s.terminate()

	#handle when connection is closed and remove client
	def handle_close(self):
		self.clients.remove_all()
		self.s.close()
		print 'Sockets closed'
Beispiel #10
0
def main():
    freeze_support()

    p1 = Process(target=kakao_crawler,
                 args=(
                     restaurant_list,
                     "영광보쌈",
                     driver,
                 ))
    p2 = Process(target=naver_crawler,
                 args=(
                     restaurant_list,
                     "영광보쌈",
                     driver,
                 ))

    p1.daemon = True
    p2.daemon = True

    p1.start()
    p2.start()

    p1.close()
    p2.close()

    p1.join()
    p2.join()
Beispiel #11
0
    def runner(self, StreamType: Type, stream_callback):
        """
        Instantiates a sounddevice.InputStream and calls for a parallel process
        if any monitoring is set up.
        Then turn on the switch Event, and starts the stream.
        Waits for it to finish, unset the event
        And terminates the process

        :return:
        :rtype:
        """
        with StreamType(samplerate=self.samplingRate,
                        blocksize=self.blockSize,
                        device=self.device,
                        channels=self.numChannels,
                        dtype=self.dataType,
                        latency='low',
                        dither_off=True,
                        callback=stream_callback) as stream:
            if self.switch.is_set():
                t = Process(target=self.monitoring)
                t.start()
            self.running.set()
            stream.start()
            while stream.active:
                pass
            stream.stop()
            self.running.clear()
            if self.switch.is_set():
                t.join()
                t.close()
            self.queue.close()
        return
Beispiel #12
0
    def LoadImageFromURL(self, url, folder):
        """
        Load Image from URL

        Starts the thread to get URL

        Args:
            url: URL to get
            target: Image destination file

        If no url is set uses one from config file
        """
        self.lastImageSaved = ''
        captureTime = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())
        target = folder + captureTime + '.jpg'
        if not url:
            url = self.URLImageSource
        event = Event()
        action_process = Process(target=self.ReadURL, args=(event, url, target))
        action_process.start()
        action_process.join(timeout=self.TimeoutLoadImage)
        action_process.close()

        logtime = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())
        if event.is_set():
            if self.VerifyImage(target) == True:
                from shutil import copyfile
                copyfile(target, folder + 'capture.jpg')
                result = ''
            else:
                result = 'Error - Image file is corrupted'
        else:
            result = 'Error - Problem during Image load (file not exists or timeout)'
        return (result, logtime)
Beispiel #13
0
def run_plane_offline(plane, tiff_folder, params, x_start, x_end, 
                      n_init=500, max_frames=30000, add_rate=1, **kwargs):
    
    q = Queue()
    xslice = slice(x_start, x_end)
    tiff_files = Path(tiff_folder).glob('*.tif*')
    mm3d_file = str(list(Path(tiff_folder).glob('*makeMasks3D_img.mat'))[0])
    
    if not mm3d_file:
        logger.error(f'No makeMasks3D found at: {mm3d_file}')
        raise FileNotFoundError
    
    init_list, nchannels, nplanes, tslice = prepare_init(plane, n_init, tiff_files)    
    
    # once you have gotten the tiffs for init, run the queue
    print('starting initialization...')
    worker = RealTimeQueue(init_list, plane, nchannels, nplanes, params, q,
                           num_frames_max=max_frames, Ain_path=mm3d_file,
                           xslice=xslice, **kwargs)
        
    print('starting queue...')
    queue_p = Process(target=append_to_queue, args=(q, tiff_folder, tslice, add_rate))
    # queue_p = Thread(target=append_to_queue, args=(q, tiff_folder, tslice, add_rate))
    queue_p.start()
    
    print('starting worker...')
    result = worker.process_frame_from_queue()
    queue_p.join()
    queue_p.close()
    print('done!')
    
    return result
Beispiel #14
0
def main():
    while True:
        client_socket, client_info = server_socket.accept()
        p = Process(target=clientdeal, args=(client_socket, client_info))
        p.start()
    p.close()
    server_socket.close()
class TimedProgress:
    """Show a progress bar and measure elapsed time until .end() is called"""
    def __init__(self, seconds, prefix=''):
        from .config import SHOW_PROGRESS
        self.SHOW_PROGRESS = SHOW_PROGRESS
        if self.SHOW_PROGRESS:
            self.p = Process(target=progress_bar, args=(seconds, prefix))
            self.p.start()

        self.stats = {'start_ts': datetime.now(), 'end_ts': None}

    def end(self):
        """immediately end progress, clear the progressbar line, and save end_ts"""

        end_ts = datetime.now()
        self.stats['end_ts'] = end_ts

        if self.SHOW_PROGRESS:
            # terminate if we havent already terminated
            self.p.terminate()
            self.p.join()
            self.p.close()

            # clear whole terminal line
            try:
                sys.stdout.write('\r{}{}\r'.format((' ' * TERM_WIDTH()),
                                                   ANSI['reset']))
            except (IOError, BrokenPipeError):
                # ignore when the parent proc has stopped listening to our stdout
                pass
def runImageProcessingProcessPool(applicationSettings, imageProcessorSettings,
                                  stream, arduinoConnector):
    print("Using Multiprocessing")
    #processList = createProcesses(imageProcessors, stream, arduinoConnector)
    log_to_stderr()
    logger = get_logger()
    logger.setLevel(logging.DEBUG)
    logger.info("Setup logger")
    logger.debug(imageProcessorSettings)
    processList = []
    for i in range(0, imageProcessorSettings["numberOfImageProcessors"]):
        logger.info("Starting new process")
        settings = [applicationSettings, imageProcessorSettings]

        process = Process(target=runImageProcessorAsProcess,
                          args=(settings, arduinoConnector, stream))
        processList.append(process)
        process.start()

    logger.info("Stuff initialized")

    while True:
        try:
            time.sleep(1)
            print("Main app is sleeping")
        except KeyboardInterrupt:
            print("Keyboard Interrupt received")
            for process in processList:
                process.close()
            arduinoConnector.shutdownConnection()
            stream.stop()
            break
    return
Beispiel #17
0
    def _download(url: str, output: str, file_size: int, chunk_size: int,
                  number_of_processes: int) -> List[Tuple[int, int]]:
        processes = []
        queues = []
        results = []

        sub_chunk_size = ceil(chunk_size / number_of_processes)
        for chunk in range(FILE_START, file_size, chunk_size):
            chunk_size = min(chunk_size, file_size - 1 - chunk)
            queue = Queue()
            p = Process(
                target=MultiProcessAsyncDownloaderStrategy.download_chunks,
                args=(queue, str(sub_chunk_size), str(chunk_size), url, output,
                      str(chunk)))
            p.start()

            processes.append(p)
            queues.append(queue)

        for q in queues:
            results.append(q.get())
            q.close()
        for p in processes:
            p.join()
            p.close()
        return results
class CelRecognizerWorker(QObject):
    update = Signal(list)

    def __init__(self):
        super().__init__()
        self.process = None

        self.timer = QTimer()
        self.timer.timeout.connect(self.check_for_results)
        self.timer.setInterval(1000)
        self.timer.start(1000)
    
    def submit(self, cel_path, video_path):
        # Cancel an existing process if it exists
        self.cancel()
        
        # Start a new process to run the CelRecognizer
        self.pipe, child_conn = Pipe()
        self.process = Process(
            target=_recognizer_worker_func,
            args=(child_conn, cel_path, video_path))
        self.process.start()
    
    def check_for_results(self):
        if self.process is not None:
            while self.pipe.poll():
                results = self.pipe.recv()
                self.update.emit(results)

    def cancel(self):
        if self.process is not None:
            self.process.terminate()
            self.process.join()
            self.process.close()
            self.process = None
Beispiel #19
0
class Runner(ABC):
    connection: Queue = Queue()
    lock: Lock = Lock()

    def __init__(self, config: Config, logger: Logger, *runnerArgs):
        self.config = config
        self.logger = logger
        self.process = Process(target=self.execute,
                               args=(Runner.connection, Runner.lock) +
                               runnerArgs)

    @abstractmethod
    def execute(self, queueConn: Queue, lock: Lock, *args):
        pass

    def start(self):
        self.process.start()
        self.logger.log(f"Started {type(self).__name__} process", LogType.INFO)

    def waitUntilFinished(self):
        self.process.join()

    def close(self):
        self.process.kill()
        self.process.close()
        self.logger.log(f"Stopped {type(self).__name__} process", LogType.INFO)
Beispiel #20
0
    def _run_simulation(self):
        # Due to JPype limitations, the JVM cannot be restarted by the same process.
        # https://jpype.readthedocs.io/en/latest/install.html#known-bugs-limitations
        # To work around this, we spawn a new process each time we want to run a simulation.
        q = Queue()
        p = Process(target=_process_simulation, args=(
            self._ork_file,
            self._random_seed,
            self._drogue_deployment_time,
            self._main_deployment_time,
            self._drogue_component_name,
            self._main_component_name,
            q
        ))

        p.start()
        result = q.get()
        p.join()
        p.close()

        if isinstance(result, Exception):
            raise result

        data, events = result

        assert events[FlightEvent.IGNITION][0] == 0
        assert events[FlightEvent.LAUNCH][0] == 0
        assert abs(data[FlightDataType.TYPE_TIME][-1] - events[FlightEvent.GROUND_HIT][0]) < 0.1
        assert abs(data[FlightDataType.TYPE_TIME][-1] - events[FlightEvent.SIMULATION_END][0]) < 0.1
        return data, events
Beispiel #21
0
async def eventlog_client(
        client_session: ClientSession) -> AsyncIterator[EventLogClient]:
    """
    Note: adding this fixture to a test: a complete resotoeventlog process is started.
          The fixture ensures that the underlying process has entered the ready state.
          It also ensures to clean up the process, when the test is done.
    """
    multiprocessing.set_start_method("spawn", True)
    process = Process(
        target=run,
        args=(["--debug", "--no-tls", "--port", "8951"], ),
    )
    process.start()
    ready = False
    count = 10
    while not ready:
        await sleep(0.5)
        count -= 1
        if count == 0:
            raise AssertionError("Process does not came up as expected")
        with suppress(Exception):
            async with client_session.get(
                    "http://localhost:8951/system/ready"):
                ready = True
    yield EventLogClient("http://localhost:8951", client_session)
    # terminate the process
    process.terminate()
    process.join(5)
    # if it is still running, kill it
    if process.is_alive():
        process.kill()
        process.join()
    process.close()
Beispiel #22
0
def LEASE_INIT():
    pl = [
        INFO_BOX_TPE_INIT, INFO_BOX_NTC_INIT, HOUSE_BOX_TPE_INIT,
        HOUSE_BOX_NTC_INIT, IMG_TPE_INIT, IMG_NTC_INIT
    ]
    p = [None] * 6

    p1 = Process(target=LEASE_TPE_INIT)
    p2 = Process(target=LEASE_NTC_INIT)
    # start colleting data
    p1.start()
    p2.start()
    # p1, p2 須執行完才能往下進行
    p1.join()
    p2.join()
    p1.close()
    p2.close()

    try:
        for i in range(0, 6):
            p[i] = Process(target=pl[i])
            p[i].start()
        for i in range(0, 6):
            p[i].join()
        for i in range(0, 6):
            p[i].close()
    except Exception as e:
        print(e)
    finally:
        print("Lease data collection complete")
Beispiel #23
0
    def run(self):
        '''!
            Run the stages on multiple process.

            Locks the code until the stages end
        '''

        process = []
        
        for stage in self.stages:
            p = Process(target=self._run_stage, args=(stage,))
            p.start()

            process.append(p)

        while 1:
            try:
                pass
            except KeyboardInterrupt:
                break

        print("TERMINANDO")

        for p in process:
            p.terminate()
            p.join(1)
            p.close()
Beispiel #24
0
 def train(self, train_x, train_y, valid_x, valid_y, mlb):
     self.model_cnf['cluster']['groups_path'] = self.groups_path
     cluster_process = Process(target=build_tree_by_level,
                               args=(self.data_cnf['train']['sparse'], self.data_cnf['train']['labels'], mlb),
                               kwargs=self.model_cnf['cluster'])
     cluster_process.start()
     self.train_level(self.level - 1, train_x, train_y, valid_x, valid_y)
     cluster_process.join()
     cluster_process.close()
class ConvertImageProcess:
    """Represents an independent process for an image conversion process.
    Key feature includes the ability to track progress of a conversion process

    Properties
    ----------
    progress : multiprocessing.Value
        - stores the progress of the image conversion
    image_path : string
        - stores image_path 

    Methods
    --------
    terminate_process
        - terminates the conversion process and closes it
    get_progress
        - returns the current progress of the conversion
    """
    def __init__(self,
                 image_path,
                 output_path="output.jpg",
                 override=False,
                 image_reducer=10,
                 fontSize=10,
                 spacing=1.1,
                 maxsize=None,
                 chars=" .*:+%S0#@",
                 logs=False,
                 threads=4):
        self.progress = Value("f", 0, lock=True)
        self.image_path = image_path
        self.output_path = output_path
        self._process = Process(
            target=convert_image_path_and_save,
            args=(image_path, output_path, override, image_reducer, fontSize,
                  spacing, maxsize, chars, logs, threads, self.progress))

    def get_process(self):
        return self._process

    def start_process(self):
        self._process.start()

    def join_process(self):
        self._process.join()

    def terminate_process(self):
        self._process.kill()
        self._process.join()
        self._process.close()

    def get_progress(self):
        with self.progress.get_lock():
            return self.progress.value
Beispiel #26
0
def motor():
    currentState = 0

    # Set up the motor object
    car = u.Motor()
    car.build()

    # Set up the OpenCV Process with pipe
    parent_conn, child_conn = Pipe()
    p = Process(target=oracling, args=(child_conn, ))
    p.start()

    # What the different states mean
    states = [
        "Full Speed", "0 left", "1 left", "2 left", "3 left", "4 left",
        "0 right", "1 right", "2 right", "3 right", "4 right"
    ]

    car.start()

    while True:
        val = parent_conn.recv()

        # Decide the next state
        if (val == 0):
            currentState = val
            car.setSpeed(motor1=True, motor2=True)
            print('Val = 0, right speed is %f, car left speed is %f' %
                  (car.currentRight, car.currentLeft))

        elif (0 < val <= 5):
            currentState = val - 1
            car.turnLeft(hardness=currentState)
            print('Val = %d, right speed is %f, car left speed is %f' %
                  (val, car.currentRight, car.currentLeft))

        elif ((6 <= val <= 10)):
            currentState = val
            car.turnRight(hardness=val - 6)
            print('Val = %d right speed is %f, car left speed is %f' %
                  (val, car.currentRight, car.currentLeft))

        elif (val == 13):
            currentState = 13
            car.stop()
            print('Val = %d, right speed is %f, car left speed is %f' %
                  (val, car.currentRight, car.currentLeft))
            p.close()

        else:
            currentState = 11
            car.stop()
            print('Val = %d, right speed is %f, car left speed is %f' %
                  (val, car.currentRight, car.currentLeft))
Beispiel #27
0
def main():
    fifo_fd = open(Fifo, "r")
    line = fifo_fd.readline()
    lector_conn, escritor_conn = Pipe()
    pid = Process(target=hijo, args=(lector_conn, ))
    pid.start()
    escritor_conn.send(line)
    escritor_conn.close()
    time.sleep(4)
    pid.join()
    pid.close()
    sys.exit(0)
def main():
    #创建socket
    server_socket = socket(AF_INET, SOCK_STREAM)
    #server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind(("",8899))
    server_socket.listen(128)
    while True:
        client_socket, client_info = server_socket.accept()
        p = Process(target=clientdeal, args=(client_socket, client_info))
        p.start()
        client_socket.close() #在主进程中将新创建的socket关闭,否则客户端访问的话会一直卡住
    p.close()
    server_socket.close()
Beispiel #29
0
 def start_and_wait_sub_process(target) -> Optional[int]:
     p = Process(target=target, args=(config, ignore_errors))
     p.start()
     exit_code = None
     try:
         p.join(500)
         exit_code = p.exitcode
     except subprocess.TimeoutExpired:
         p.kill()
         raise
     finally:
         p.close()
     return exit_code
def main(args):
    if not Path(args.synthtext_folder).exists():
        logger.error('synthtext_folder does not exist!')
        raise FileNotFoundError

    manager = Manager()
    queue = manager.Queue(maxsize=QUEUE_SIZE)

    # config data writer parallel process, read cropped data from queue, then save it to lmdb or images/txt file
    if args.data_format == 'lmdb':
        writer_process = Process(target=lmdb_writer,
                                 name='lmdb writer',
                                 args=(args.lmdb_path, queue),
                                 daemon=True)
    elif args.data_format == 'images_with_gt_file':
        Path(args.images_folder).mkdir(parents=True, exist_ok=True)
        writer_process = Process(target=images_with_gt_file_writer,
                                 name='images_with_gt_file writer',
                                 args=(args.images_folder, args.gt_file,
                                       queue),
                                 daemon=True)
    else:
        Path(args.images_folder).mkdir(parents=True, exist_ok=True)
        writer_process = Process(
            target=lmdb_and_images_with_gt_file_writer,
            name='lmdb_and_images_with_gt_file_writer writer',
            args=(args.lmdb_path, args.images_folder, args.gt_file, queue),
            daemon=True)
    writer_process.start()

    logger.info('{} writer is started with PID: {}'.format(
        args.data_format, writer_process.pid))

    # config synthtext data reader jobs
    pool = Pool(processes=WORKERS, maxtasksperchild=10000)
    try:
        logger.info('Start cropping...')
        # crop synthtext, and put cropped data into queue
        synthtext_reader(args.synthtext_folder, queue, pool)
        queue.put('Done')
        pool.close()
        pool.join()

        writer_process.join()
        writer_process.close()
        logger.info('End cropping.')
    except KeyboardInterrupt:
        logger.info('Terminated by Ctrl+C.')
        pool.terminate()
        pool.join()
Beispiel #31
0
def tryforkwindows(n):
    if n == 1:
        print('Parent process %s.' % os.getpid())
        sec = 3
        p = Process(target=run_proc, args=('test', sec))
        print('Child process will start ...')
        p.start()
        childlast = 15
        while childlast >= 0:
            print('# Parent (%s) and its child (%s).' % (os.getpid(), p.pid))
            time.sleep(1)
            childlast -= 1

        # Not successful!
        # os.kill(p.pid, signal.CTRL_C_EVENT)
        # p.join()

        print('Parent (%s) kills child (%s)' % (os.getpid(), p.pid))
        p.terminate()
        print('Child process end.')

    if n == 2:
        print('Parent process %s.' % os.getpid())
        process_num = 16
        p = Pool(process_num)
        for i in range(process_num):
            p.apply_async(long_time_task, args=(i,))

        print('Waiting for all subprocesses done...')
        p.close()
        p.join()
        print('All subprocesses done.')

    if n == 3:
        cmd = 'ping www.baidu.com'
        print(cmd)
        r = subprocess.call(cmd.split())
        print('Exit code = ', r)

    if n == 4:
        q = Queue()
        pw = Process(target=q_write, args=(q,))
        pr = Process(target=q_read, args=(q,))
        pr.start()
        pw.start()
        pw.join()
        pr.terminate()

    return
Beispiel #32
0
def get_box_token():

    # Don't show starting message from server
    cli.show_server_banner = lambda *_: None

    # Don't show logs from server
    log = logging.getLogger('werkzeug')
    log.disabled = True

    # Set up authentication parameters
    base_url = "https://account.box.com/api/oauth2/authorize"
    client_id = "kztczhjoq3oes38yywuyfp4t9tu11it8"
    client_secret = "a5xNE1qj4Z4H3BSJEDVfzbxtmxID6iKY"
    login_url = f"{base_url}?client_id={client_id}&response_type=code"

    # Start the server
    server = Process(target=app.run, kwargs={"port": "8003"})
    server.start()

    # Send the user to the "Grant access" page
    webbrowser.open(login_url)
    login_msg = "Please login to Box on the webpage that was just opened and grant access for cptac to download files through your account. If you accidentally closed the browser window, press Ctrl+C and call the download function again."
    print(login_msg)

    # Get the temporary access code from the server on the child process
    temp_code = parent_conn.recv()

    # Give the browswer time to grab the response page
    time.sleep(1)

    # End the server process
    server.terminate()
    server.join()
    server.close()

    # Use the temporary access code to get the long term access token
    token_url = "https://api.box.com/oauth2/token"

    params = {
        'grant_type': 'authorization_code',
        'code': temp_code,
        'client_id': client_id,
        'client_secret': client_secret,
    }

    auth_resp = requests.post(token_url, data=params)
    access_token = auth_resp.json()["access_token"]

    return access_token
import os,time,random

def long_time_task(name):
	print('Run task %s (%s)...' %(name,os.getpid()))
	start = time.time()
	time.sleep(random.random() * 3)
	end = time.time()
	print('Tasks %s runs %0.2f sconds.' %(name,(end-start)))
	
if __name__=='__main__':
	print('Parent process %s.' % os.getpid())
	p = Pool(4)
	for i in range(5):
		p.apply_async(long_time_task,args=(i,))
	print('Waiting for all subprocesses done...')
	p.close() #调用join()之前必须先调用close(),调用close()之后就不能继续添加新的Process了。
	p.join()  #对Pool对象调用join()方法会等待所有子进程执行完毕
	print('All subprocesses done.')
    #task 0,1,2,3是立刻执行的,而task 4要等待前面某个task完成后才执行,这是因为Pool的默认大小在我的电脑上是4,因此,最多同时执行4个进程。这是Pool有意设计的限制,并不是操作系统的限制。
	#由于Pool的默认大小是CPU的核数,如果你不幸拥有8核CPU,你要提交至少9个子进程才能看到上面的等待效果。
	

#子进程 
print("==============subprocess=====================")
import subprocess
r=subprocess.call(['nslookup','www.baidu.com'])
print('Exit code:',r)



#进程间通信
Beispiel #34
0
def long_time_task(name):
    print "Run task %s (%s)" % (name, os.getpid())
    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.getpid()
    p = Pool(9)
    for i in range(5):
        p.apply_async(long_time_task, args=(i,))
    print "Wait for all subprocess done..."
    p.close()  # close 后进不能在加入process
    p.join()
    print "All subprocess end..."

# 进程间通讯 Queue 或 Pipes
from multiprocessing import Process, Queue
import os, random, time


def write(q):
    for value in ["a", "b", "c"]:
        print "Put %s to queue..." % value
        q.put(value)
        time.sleep(4)

Beispiel #35
0
print '\n进程池'

def long_time_task(name):
    print 'Run task %s (%s)...' %(name, os.getpid())
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print 'Task %s runs %0.2f seconds.' %(name, (end - start))
    
if __name__ == '__main__':        #表示程序作为主程序执行,而不是使用import作为模块导入
    print 'Parents process %s.' %os.getpid()
    p = Pool()
    for i in range(9):
        p.apply_async(long_time_task, args = (i, ))
    print 'Waiting for all subprocesses done...'
    p.close() # 调用close()之后就不能继续添加新的Process了。
    p.join()  # 对Pool对象调用join()方法会等待所有子进程执行完毕,调用join()之前必须先调用close()。
    print 'All subprocesses done.'   
    
    
print '\n多线程'

import threading
# 新线程执行代码:
def loop():
    print 'thread %s is running...' %threading.current_thread().name
    n = 0
    while n < 5:
        n = n + 1
        print 'thread %s >>> %s ' %(threading.current_thread().name, n)
        time.sleep(1)
Beispiel #36
0
import os, time, random

def long_time_task(name):
    print('run task %s (%s)...' % (name, os.getpid()))
    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.getpid())
    p = Pool(4) # run 4 processes each time
    for i in range(5):
        p.apply_async(long_time_task, args = (i,))
    print('waiting for all subprocesses done...')
    p.close() # close is necessary before join
    p.join()
    print('all subprocesses done.')

# When running this in spyder, it keeps waiting like forever.

# It works in cmd.
# open cmd
# python D:\python\python\Note7-liao.py
# (you need to input the absolute path)
#%%

# subprocess

import subprocess
Beispiel #37
0
if __name__ == '__main__':
    # 创建进程
    print('Parent process %s.' % (os.getpid()))
    p = Process(target=run_proc, args=('test',))  # 创建子进程,需要函数,参数是元组
    print('Child process will start.')
    p.start()  # 启动进程
    p.join()  # 等待子进程结束才继续运行
    print('Child process end.')

    # 进程池
    print('Parent process %s.' % os.getpid())
    p = Pool(4)
    for i in range(5):
        p.apply_async(long_time_task, args=(i,))
    print('Waiting for all subprocesses done.')
    p.close()  # close 之后不能够再添加新的进程
    p.join()  # 在join之前必须调用close
    print('All done')

    # 子进程
    print('$ nslookup www.python.org')
    r = subprocess.call(['nslookup', 'www.python.org'])
    print('Exit code:', r)

    # 进程间通信
    q = Queue()

    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    pw.start()  # 启动写进程
    pr.start()  # 启动读进程
Beispiel #38
0
import os, time, random

def long_time_task(name):
    print('Run task %s (%s)...' % (name, os.getpid()))
    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.getpid())
    p = Pool(4)
    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.')
"""对Pool对象调用join()方法会等待所有子进程执行完毕,调用join()之前必须先调用close(),调用close()之后就不能继续添加新的Process了。
请注意输出的结果,task 0,1,2,3是立刻执行的,而task 4要等待前面某个task完成后才执行,这是因为Pool的默认大小在我的电脑上是4,因此,最多同时执行4个进程。这是Pool有意设计的限制,并不是操作系统的限制。如果改成:
p = Pool(5)
就可以同时跑5个进程。
由于Pool的默认大小是CPU的核数,如果你不幸拥有8核CPU,你要提交至少9个子进程才能看到上面的等待效果。"""

#子进程
"""很多时候,子进程并不是自身,而是一个外部进程。我们创建了子进程后,还需要控制子进程的输入和输出。
subprocess模块可以让我们启动一个子进程,然后控制其输入和输出
下面的例子演示了如何在Python代码中运行命令nslookup www.python.org,这和命令行直接运行的效果是一样的:"""
import subprocess
print('$ nslookup www.python.org')
r = subprocess.call(['nslookup','www.python.org'])
Beispiel #39
0
'''若要启动大量子进程,可用进程池的方式批量创建子进程'''
from  multiprocessing import Pool
def long_time_task(name):
	print('Run task %s (%s)...' % (name, os.getpid()))
	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.getpid()) #os.getpid()可获取父进程的id
	p=Pool(3) #创建一个进程池p,包含3个进程
	for i in range(5):
		p.apply_async(long_time_task,args=(i,))
	print('Waiting for all subprocesses done...')
	p.close() #关闭pool
	p.join() #等待所有子进程结束后在继续往下执行,用于进程间同步
	print('All subprocesses done')

'''subprocess模块可方便地启动一个子进程,然后控制其输入和输出'''
import subprocess
print('$ nslookup www.python.org')
r=subprocess.call(['nslookup','www.python.org'])
print('Exit code:', r)

'''进程间通信,multiprocessing模块包装了底层的机制,提供了Quene,Pipes
等多种方式交换数据'''
from  multiprocessing import Process,Queue
def write(q):
	print('Process to write: %s' % os.getpid())
	for value in ['A', 'B', 'C']: