繁体   English   中英

如何从一个 Python 脚本发送信号以触发另一个脚本?

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

我目前正在运行一个 python 脚本,该脚本与 API 交互以获取库存价格,当满足特定条件时,我发送一个发布请求以根据其唯一标识号将此项目放入购物车。

我遇到的问题是,在商品放入购物车后,我无法再与 API 互动以发布购买信息,而我必须通过结帐流程与 go 互动。 这可以通过 Selenium 轻松完成,但这里的固有问题是速度,因为我必须启动 chrome window,导航到站点,登录等。

相反,我想做的是有两个单独的 python 脚本,一个运行获取并将项目添加到结帐购物车,另一个运行 selenium,已经登录到购买帐户。 将商品放入购物车后,脚本将向另一个(已在运行,正在等待命令)selenium 机器人发送信号。

这对于节点来说是可能的,但之前没有做过这样的事情——如果有人能指出我正确的方向,我将不胜感激!

我想你可以使用 Python 中的multiprocessing模块在单独的进程中运行这两个脚本,然后使用multiprocessing模块中的Queue class 在进程之间进行通信。

例如,这里的buy在从第一个脚本接收到信号后被调用:

将商品添加到购物车的脚本:

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()

启动购买过程的另一个脚本。

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
            # ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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