• python多进程(multiprocessing调用类函数)


    #coding=utf-8
    
    import os
    import multiprocessing
    from time import ctime, sleep
    from selenium import webdriver
    
    
    class testClass(object):
    
        def worker(self, interval, browser="Chrome", url="http://loginurl"):
            driver = eval("webdriver.%s()" % browser)
            driver.get(url)
            driver.find_element_by_id("txtUserName").send_keys("username")
            driver.find_element_by_id("txtPwd").send_keys("password")
            sleep(1)
            driver.find_element_by_id("btnLogin").click()
            print("I was at the %s %s" % (browser, ctime()))
            sleep(interval)
            print("end worker_2")
    
    
    if __name__ == "__main__":
        for i in range(2):
            a = testClass()
            p = multiprocessing.Process(target=a.worker, args=(2, "Chrome"))
            p.start()
            sleep(2)
    
        print("The number of CPU is:" + str(multiprocessing.cpu_count()))
        for p in multiprocessing.active_children():
            print("child   p.name:" + p.name + "	p.id" + str(p.pid))
        print("END!!!!!!!!!!!!!!!!!")

    如果要接收返回值,那么按下面脚本写:

    import requests
    import multiprocessing
    from loguru import logger
    
    
    def worker(url, headers, q):
        t = 0
        r = requests.get(url=url, headers=headers)
        text = eval(r.text)['Data'][-1]
        # print(text)
        q.put(text)
    
    
    if __name__ == '__main__':
        q = multiprocessing.Queue()
        jobs = []
        p = multiprocessing.Process(target=worker, args=(url1, headers, q))
        jobs.append(p)
        p.start()
        p = multiprocessing.Process(target=worker, args=(url2, headers, q))
        jobs.append(p)
        p.start()
        for p in jobs:
            p.join()
    
        results = [q.get() for j in jobs]
        if results[0] == results[1]:
            pass
        else:
            logger.error("验证不通过!"))

    参考资料:https://blog.csdn.net/springlustre/article/details/88703947

  • 相关阅读:
    Redis的配置与数据类型
    CSRF Failed: CSRF token missing or incorrect
    腾讯防水墙实现验证码
    Rest_Framework常用插件
    rest_framework序列化
    Nginx项目部署
    Nginx
    Django设置允许跨域请求
    Rest_Framework的视图与路由
    Rest_Framework
  • 原文地址:https://www.cnblogs.com/carlvine/p/9258903.html
Copyright © 2020-2023  润新知