GithubHelp home page GithubHelp logo

desire2020 / py-stdio-ipc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from abcdabcd987/py-stdio-ipc

1.0 3.0 0.0 14 KB

Python IPC based on stdin / stdout

License: MIT License

C++ 19.17% Python 80.83%

py-stdio-ipc's Introduction

py-stdio-ipc

Python IPC based on stdin / stdout

Example

Usage: ./sample_judge.py AI1Path AI2Path

$ clang++ -Wall sample_ai_stderr.cc -o sample_ai_stderr
$ clang++ -Wall sample_ai_tle.cc -o sample_ai_tle
$ clang++ -Wall sample_ai_re.cc -o sample_ai_re
$ clang++ -Wall sample_ai.cc -o sample_ai
  
$ ./sample_judge.py ./sample_ai ./sample_ai
{'ai1_err': '', 'ai1_id': 0, 'ai2_err': '', 'ai2_id': 1, 'winner': 2}
 
$ ./sample_judge.py ./sample_ai ./sample_stderr
{'ai1_err': '', 'ai1_id': 0, 'ai2_err': '', 'ai2_id': 1, 'winner': 1}
  
$ ./sample_judge.py ./sample_ai ./sample_tle
{'ai1_err': '', 'ai1_id': 0, 'ai2_err': 'timeout', 'ai2_id': 1, 'winner': 1}
  
$ ./sample_judge.py ./sample_ai ./sample_re
{'ai1_err': '',
 'ai1_id': 1,
 'ai2_err': "wrong format. your output: ''",
 'ai2_id': 0,
 'winner': 1}

API

All of the following APIs will block the main thread. See sample_judge.py and sample_ai.cc to know how to use them.

stdio_ipc.ChildProcess(args, save_stdin_path, save_stdout_path, save_stderr_path)

Create a child process with program arguments args. Save stdio into files.

stdio_ipc.ChildProcess.send(content)

Send content to the child process. content MUST ends with \n.

stdio_ipc.ChildProcess.recv(timeout)

Receive output from the child process. Timeout if not response correctly with timeout second(s). The output of child process MUST ends with END\n and remember to flush, otherwise it may timeout.

stdio_ipc.ChildProcess.exit()

Kill the child.

(Deprecated) stdio_ipc.ChildProcess.save_stdio(path_stdin, path_stdout, path_stderr)

Save the stdio history to path_stdin, path_stdout and path_stderr respectively. END\n will be removed from the result of stdout.

About the example

I wrote an example to show how to use the library. The example is an AI game of two players. Each player shows a number (0 ~ 5) using his fingers while yell another number (0 ~ 10) at the same time. Somebody wins if what he yelled equals to the sum of what both of the two showed in hands.

  • sample_judge.py shows how can a judge program be written, using this library.
  • sample_ai.cc shows how should a player communicate with the judge. Note the end() function.
  • sample_ai_tle.cc and sample_ai_re.cc are both used to demonstrate what will happen if a player program misbehave.
  • sample_ai_stderr.cc is a program that prints much log into stderr.

py-stdio-ipc's People

Contributors

abcdabcd987 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.