如何在脚本之间建立线程并传递变量?

时间:2018-07-05 20:16:18

标签: python multithreading python-multithreading

我有my main_script.py和一个second_script.py

我想从激活用户的表中获取并将其传递给我的第二个脚本

cursor = db.cursor()
sql_select_count = "SELECT * FROM table WHERE activated = 1;"
cursor.execute(sql_select_count)
results = cursor.fetchall()
    for row in results:

据此,我想启动另一个脚本的线程,将其表中的一列作为参数动态传递。

这可能吗?

编辑: 这是我的第二个脚本

    from selenium import webdriver
import pymysql
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
pymysql.install_as_MySQLdb()
db = pymysql.connect("localhost", "root", "password", "mysql")
cursor = db.cursor()
sql_select_cookie = "SELECT cue_cookie FROM wa_cuentas WHERE cue_id = '%d'" % 1
sql_select_browser = "SELECT cue_url, cue_session_id FROM wa_cuentas where cue_id = '%d'" % 1
def iniciar_browser(cookie):
    try:
        chrome_options = Options()
        chrome_options.add_argument("user-data-dir=" + cookie)
        driver = webdriver.Chrome(chrome_options=chrome_options)
        driver.get("http://web.whatsapp.com")
        url = driver.command_executor._url
        session_id = driver.session_id
        sql_update = ("UPDATE wa_cuentas SET cue_url='"
                      "" + url + "',cue_session_id='"
                                 "" + session_id + "',cue_online=1 WHERE cue_cookie='" + cookie + "'")
        cursor.execute(sql_update)
        db.commit()
        return driver
    except:
        print("no se pudo arrancar el browser")

def reconectar_browser(cookie):
    try:
        cursor.execute(sql_select_browser)
        results = cursor.fetchall()
        for row in results:
            url = row[0]
            session_id = row[1]
        driver = webdriver.Remote(command_executor=url, desired_capabilities={})
        driver.session_id = session_id
        return driver
    except:
        return iniciar_browser(cookie)
        print("no existia conexion")
        pass
try:
    # seleccionamos los mensajes que aun no fueron enviados
    cursor.execute(sql_select_cookie)
    cookie = cursor.fetchone()
    cookie = cookie[0]
    driver = reconectar_browser(cookie)

except:
    print("no se pudo traer los datos")
db.close()

0 个答案:

没有答案