# Python communicate with subprocess between threads

## Content

Use python subprocess to communicate between python scripts.

```# script.py import subprocess   process = subprocess.Popen(     ['python', 'my_other_script.py'], shell=False,     stdin=subprocess.PIPE, stdout=subprocess.PIPE) result = process.stdout.readline() print(result)``` ```# my_other_script.py print('hello, world!')``` ```\$ python script.py hello, world!```

Here's an example game in which the player tries to guess the dealer's number.

```# Dealer (dealer.py) import random import sys   MIN_NUMBER = 1 MAX_NUMBER = 10   def main():     while True:         player_bet = int(sys.stdin.readline())         if player_bet == 0:             sys.stdout.write('Ok. Thanks for playing.\n')             sys.stdout.flush()             break         number = random.randint(MIN_NUMBER, MAX_NUMBER)         if player_bet == number:             sys.stdout.write('number is {0}: player wins!\n'.format(number))         else:             sys.stdout.write('number is {0}: dealer wins\n'.format(number))         sys.stdout.flush()     if __name__ == '__main__':     main()``` ```# Player (player.py) import random import subprocess import sys   import dealer   process = subprocess.Popen(     ['python', 'dealer.py'], shell=False,     stdin=subprocess.PIPE, stdout=subprocess.PIPE)   for i in xrange(8):     bet = random.randint(dealer.MIN_NUMBER, dealer.MAX_NUMBER)     sys.stdout.write('player betting {0}\n'.format(bet))     process.stdin.write('{0}\n'.format(bet))     result = process.stdout.readline()     sys.stdout.write(result)   process.stdin.write('0\n') result = process.stdout.readline() sys.stdout.write(result)``` ```\$ python player.py player betting 4 number is 4: player wins! player betting 2 number is 8: dealer wins player betting 2 number is 6: dealer wins player betting 1 number is 9: dealer wins player betting 5 number is 7: dealer wins player betting 1 number is 10: dealer wins player betting 7 number is 7: player wins! player betting 8 number is 3: dealer wins Ok. Thanks for playing.```
{"ft_lang_label":"__label__en","ft_lang_prob":0.66071206,"math_prob":0.8922079,"size":1861,"snap":"2020-24-2020-29","text_gpt3_token_len":458,"char_repetition_ratio":0.19439957,"word_repetition_ratio":0.037037037,"special_character_ratio":0.26276195,"punctuation_ratio":0.24128686,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.983183,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-05T06:45:15Z\",\"WARC-Record-ID\":\"<urn:uuid:0141606d-5042-4605-a0ed-d02a1e2fe0bb>\",\"Content-Length\":\"20607\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3bfa9689-8c0d-4994-8696-6c53c2a59b29>\",\"WARC-Concurrent-To\":\"<urn:uuid:780ec9e5-9cb5-446a-8ff3-06a0869a2f52>\",\"WARC-IP-Address\":\"107.170.213.85\",\"WARC-Target-URI\":\"https://www.darklaunch.com/python-communicate-with-subprocess-between-threads\",\"WARC-Payload-Digest\":\"sha1:QTBGGSHG7DBZTJC5OTKF3Q6DPGLRGMVZ\",\"WARC-Block-Digest\":\"sha1:PINF6GE4P4DMK3ZKQVFUIEO356EAOYT4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655887046.62_warc_CC-MAIN-20200705055259-20200705085259-00094.warc.gz\"}"}