Exemplo n.º 1
0
def main():
    t1 = threading.Thread(target=task1)
    t2 = threading.Thread(target=task2)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
Exemplo n.º 2
0
def main():
    t1 = threading.Thread(target=task1)
    t2 = threading.Thread(target=task2)
    t1.start()
    t2.start()
    t1.join()
    t2.join()

    print(global_dict)
Exemplo n.º 3
0
def validate_todo():
    global run_spinner
    global URL_todo
    global threads
    URLs_valid = set()

    print('Validating URLs ', end='')
    #Spinner thread
    sp = threading.Thread(target=spinner_print)
    sp.start()

    #URL threads
    pool = ThreadPool(threads)
    responses = pool.map(request_url, URL_todo)

    #Close pools
    pool.close()
    pool.join()
    run_spinner = False
    sp.join()
    print('Done')
    for url, response in zip(URL_todo, responses):
        if (response != -1):
            URLs_valid.add(url)
    return (URLs_valid)
Exemplo n.º 4
0
def main():
    p_list = [threading.Thread(target=test, args=(i, )) for i in range(5)]
    for i in p_list:
        i.start()
    for i in p_list:
        i.join()
    print(num)  # 应该是500000,发生了数据混乱,结果少了很多
Exemplo n.º 5
0
def main():
    task = ShutdownTask()
    t = threading.Thread(target=task.run)
    t.start()
    task.terminate()  # 结束线程
    t.join()
    print("over")
Exemplo n.º 6
0
def main():
    lock = Lock()
    p_list = [threading.Thread(target=test, args=(i, lock)) for i in range(5)]
    for i in p_list:
        i.start()
    for i in p_list:
        i.join()
    print(num)
Exemplo n.º 7
0
def main():
    t_list = [threading.Thread(target=shopping, args=(i,)) for i in range(1000)]
    print("准备开抢ing")
    for t in t_list:
        t.start()
    for t in t_list:
        t.join()
    print(f"剩余库存{count}")
Exemplo n.º 8
0
def main():
    resource = 5  # 5个筷子,5个哲学家
    locks = [threading.Lock() for i in range(resource)]  # 几个资源几个锁

    # 抢左手筷子(locks[i])和右手的筷子(locks[(i + 1) % resource])
    # 举个例子更清楚:i=0 ==> 0,1;i=4 ==> 4,0
    tasks = [
        threading.Thread(target=eat,
                         args=(locks[i], locks[(i + 1) % resource]))
        for i in range(resource)
    ]
    for t in tasks:
        t.start()
    for t in tasks:
        t.join()
Exemplo n.º 9
0
def main():
    queue = Queue()
    # 开启生产消费者线程任务
    t_list = [
        threading.Thread(target=func, args=(queue, ))
        for func in (producer, consumer)
    ]
    # 启动两个线程
    for t in t_list:
        # 设置后台线程,就算是死循环当主线程退出的时候也会退出的
        t.setDaemon(True)  # 进程是daemon属性,t.daemon=True
        t.start()
    # 等待所有任务完成
    queue.join()  # 你可以把这句话注释掉看输出
    print(f"当前队列未完成的数量:{queue.unfinished_tasks}")
Exemplo n.º 10
0
 def start(self):
     self.__terminated_event = Event()  # 为Join服务
     t = threading.Thread(target=self.__templet)
     t.setDaemon(True)  # 设置为守护线程
     t.start()
    token_writers = []
    for _, name, sym, addr in token_list[:]:  # for each
        filename = "./andreas_daily_data/" + rm_inv_filename(
            name) + "-" + rm_inv_filename(sym) + ".csv"
        if os.path.isfile(filename):
            pass
        else:
            # making new file
            with open(filename, 'w', newline="") as f:
                writer = csv.DictWriter(f,
                                        fieldnames=[
                                            'date', 'total_tx', 'total_gas',
                                            'gw_gasprice', 'gw_mean_gasprice'
                                        ])
                writer.writeheader()

    start_date = datetime.datetime(2018, 10, 1)
    # for i in list(range(273))[:]: # go backwards in time
    dates = [start_date + datetime.timedelta(days=-1 * i) for i in range(1)]
    fix_start = functools.partial(write_token_data, start_date)
    procs = []
    for date in dates:
        fix_date = functools.partial(fix_start, date)
        t = threading.Thread(target=fix_date, args=(new_lists, ))
        procs.append(t)

    for proc in procs:
        proc.start()

    for proc in procs:
        proc.join()
Exemplo n.º 12
0
from multiprocessing.dummy import threading
from socketserver import TCPServer, BaseRequestHandler


class MyHandler(BaseRequestHandler):
    def handle(self):
        print(f"[来自{self.client_address}的消息:]\n")
        data = self.request.recv(2048)
        if data:
            print(data.decode("utf-8"))
        self.request.send(
            "HTTP/1.1 200 ok\r\n\r\n<h1>TCP Server</h1>".encode("utf-8"))


if __name__ == "__main__":
    with TCPServer(('', 8080), MyHandler) as server:
        for _ in range(10):  # 指定线程数
            t = threading.Thread(target=server.serve_forever)
            t.setDaemon(True)
            t.start()
        server.serve_forever()
Exemplo n.º 13
0
 def createthread(self, args=()):
     for i in xrange(self.threadNum):
         thread = th.Thread(target=self.crawl, args=args)
         self.threadList.append(thread)
Exemplo n.º 14
0
def main():
    # t_list = [Process(target=test, args=(i, )) for i in range(1000)]
    t_list = [threading.Thread(target=test, args=(i, )) for i in range(1000)]
    for t in t_list:
        t.start()
Exemplo n.º 15
0
def main():
    t = threading.Thread(target=sleep, args=(100, ))
    t.start()
    t.join()
    print("over")