簡體   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