简体   繁体   中英

How can I send a signal from one Python script to trigger another?

I am currently running a python script that interacts with an API to fetch inventory prices and when certain conditions are met, I send a post request to place this item based on its unique identification number into a cart.

The issue I am having is that after the item is in the cart, I am no longer able to interact with the API to post a buy, instead I have to go through the checkout process. This can easily be done with Selenium, but the inherent issue here is speed because I must launch a chrome window, navigate to the site, login, etc.

Instead, what I would like to do is have two separate python scripts with one running the fetch and adding items to a checkout carts and the other running selenium, already logged into a buying account. Once an item is placed in the cart, the script will send a signal to the other (already running, awaiting a command) selenium bot.

This may be possible with node, but have not done anything like this before - if someone could point me in the right direction it would be appreciated!

I guess u can use the multiprocessing module in Python to run the two scripts in separate processes, and then use the Queue class from the multiprocessing module to communicate between the processes.

For instance, here buy is being invoked after receiving a signal from the first script:

Script to add items to the cart:

from multiprocessing import Process, Queue

def main():
    # Fetch inventory prices and add items to the cart
    # When an item is added to the cart, put a message on the queue
    queue.put("ITEM_ADDED")

if __name__ == '__main__':
    # Create a queue to communicate with the other process
    queue = Queue()

    # Start the other process
    p = Process(target=buy, args=(queue,))
    p.start()

    # Run the main script
    main()

Another script to initiate the buy process.

def buy(queue):
    # Wait for messages from the other process
    while True:
        message = queue.get()

        if message == "ITEM_ADDED":
            # Use selenium to buy the item from the cart
            # ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM