Python - Wanting to pass data from one script to another while they are both running
Asked Answered
P

3

6

So I'm making a Discord Bot that posts when a person goes live on Twitch.tv. At the moment I have a Python program that runs the bot and a program that runs a mini server to receive the data from the Twitch server(webhook). I am unsure on how to pass on the data I receive from my server to the discord bot. Both programs have to be running at the same time.

DiscordBot

import discord


client = discord.Client()




async def goes_live(data):
    print(data)
    print('Going Live')
    msg = '--- has gone live'
    await client.send_message(discord.Object(id='---'), msg)


@client.event
async def on_message(message):
    if message.author == client.user:
        return

    message.content = message.content.casefold()


@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')


client.run('---')

Web server

import web

urls = ('/.*', 'hooks')

app = web.application(urls, globals())


class hooks:

    def POST(self):
        data = web.data()
        print("")
        print('DATA RECEIVED:')
        print(data)
        print("")

        return 'OK'

    def GET(self):
        try:
            data = web.input()
            data = data['hub.challenge']
            print("Hub challenge: ", data)
            return data
        except KeyError:
            return web.BadRequest



if __name__ == '__main__':
    app.run()
Prodigious answered 7/9, 2018 at 15:8 Comment(2)
The relevant part of the documentation: Interprocess Communication and Networking. Note that discord.py already uses asyncio, so that's probably a good place to start looking.Subdued
You have 2 options: 1. Make your webhook server post discord message. 2. (I do not recommend using this method because it makes the application more hard to maintain) pub sub with redis/rabbitmq/etc.. Do not use process based approach because you will be limited to a single machine.Slating
C
3

Since both your programs are in python, and if they are related to each other enough that they always run together, you can simply use multiprocessing module: have each one of the programs run as a multiprocessing.Process instance, and give each one of them one end of a multiprocessing.Pipe, that way you can exchange informations between processes.

The architecture would look something like that main.py:

# main.py
from multiprocessing import Process, Pipe
import program_1
import program_2

program_1_pipe_end, program_2_pipe_end = Pipe()

process_1 = Process(
    target = program_1.main,
    args = (program_1_pipe_end,)
    )

process_2 = Process(
    target = program_2.main,
    args = (program_2_pipe_end,)
    )

process_1.start()
process_2.start()
#Now they are running
process_1.join()
process_2.join()
# program_1.py and program_2.py follow this model

# [...]

# instead of if __name__ == '__main__' , do:
def main(pipe_end):
    # and use that pipe end to discuss with the other program
    pass

You can find the Pipe documentation here, (inside multiprocessing documentation).

Cerenkov answered 7/8, 2020 at 11:44 Comment(0)
D
0

Are the bot and the mini server running on the same machine? In that case you just make the server writing a file to a location where the bot can access and check periodically.

Dutybound answered 7/9, 2018 at 15:13 Comment(1)
Maybe expand your answer a bit to show how that could be done.Severn
S
0

How about using a Flask server that handles the communication between the 2 programs. You could define custom endpoints that would be able to grab the data as well as send it to the discord script.

@app.route('/ep1', methods = ['GET','POST'])
def ep1():
    if request.method == 'POST':
        #do something for a POST request. 
    else:
        #do something for a GET request.

You can use this structure to construct something that listens for changes and then publishes those to the discord bot. Also hosting this server on heroku is something that you might want to consider

Schnitzler answered 7/9, 2018 at 15:18 Comment(1)
Sending the data to the discord bot is what the question's about, and you sort of gloss over it here. How exactly are you proposing to do that?Subdued

© 2022 - 2025 — McMap. All rights reserved.