吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1123|回复: 6
收起左侧

[求助] 多线程求助

  [复制链接]
weilai911 发表于 2022-5-15 10:51
[Python] 纯文本查看 复制代码
import websocket
from threading import Thread
import time
import sys
 
def on_message(ws, message):
  print(message)
 
def on_error(ws, error):
  print(error)
 
def on_close(ws):
  print("### closed, reconnecting... ###")
  ws.run_forever()

def on_open(ws):
  ws.send('[bct295cneqru]')
  def run(*args):
    while True:
      time.sleep(20)
      ws.send("2")

    print("重新连接吧")
 
  Thread(target=run).start() 
  
if __name__ == "__main__":
 
  websocket.enableTrace(False)
  host = "ws://127.0.0.1:8080"
  ws = websocket.WebSocketApp(host,
                on_message=on_message,
                on_error=on_error,
                on_close=on_close)
  ws.on_open = on_open
  ws.run_forever()

这是单线程链接ws的方法
我现把我取到的值同时连接并发送应该如何操作
具体值如下:
dl9vwa3ocusz
edt1ee6y3b2m
ectdd89xlk0t
ea4way47oh16
dcghrydcr26w
aibc11p8lcfc
edb42uh2h1h7
ee11pfceejte
ea34crwyvr5r
dilhf68m9ayk
cjr4x33616ci
ed109mgitpo0
dloi1rr4xwzk
ee2u0u9kkg2i
cl12pd9eouua
ac34utf286m7
cc4qqex3m4g1
eb64l81gyarv
ecngg4k3idki
ecvelk45hkve
eadfyhqs9i1u
dkp82puo72qv
edhsteja6qd3
da7046a1n4s0
de1h69fzsdmh
dlulengz4nh4
ec9opdgzind9
clf6hkv9z17m
ed2993li3ku4
edk9giy5oei6
ecro2st2zfux
dfmy4jkfaqp4
cibpmmao632m
ee7ck4a5d0i7
digq81bs4wn5
edu4q4d59t8i
df4r032dide8
acmnjv6e342h
df4z8y40rwlu
ac5a6k5un64x
edkv5lo9ojj5
dkbsd5rco0e6
ec27855o0x25
dgsgk7htp4zg
edbs6coahcy6
dhl55bs4pnnh
ecnra934hxm2
dkunpsrps31q
ddqgz0hfqu04
dl3asf3kq739
aeilp6wdv0w0
dk6uf7pgxxps
dkkrph2btbzx
dlcst4on9q2p
ee65rbqbw5nf
ebb8mm0spajg
cglfm6nmt4ob
dd2yltp155l4
ddu3oa2nvn90
ed4x7094pe7u
ee85y1r364jo
dbsek6s7n2bm
dkt5eusaursx
ee1vs92892k2
ecgtg232kld9
dblakip92xbl
y7s1dlz7t29
cg24xn4qy2nh
dgutm5822l8v
eaedac9zegfw
ee959jyid7f5
ea7x5ob0d7qi
dhvidp84pi32
df353c35dim6
dk38ahd7vqjc
edup7qaesy72
ee7ynrbiprrs
ee5xw304o2x4
djd7rcuadmzd
ee55c4r9u0x0
ee3smnl92apv
ee27t055s43c
ee1n0g3nznfe
ec2n7ndd6xni
ec2hoxs3ndcx
ec2692hzmh5u
ec193fbnauk4
eag1m6en8brm
cf3ur79judh5
ee3pvdwe7d17
edl61dj5biek
ee133jdyz5hq
ed14jv11c8ir
ee61nqzwnufe
ee2e7nkcpat9
ee13kemjz44y
ebmn15zhxdyf
eeex7lw5ciax
edfzi8rkr16b
ee4t0n3f9xka
ee8hfeersb06
dgpn7qupt5pp
detn1b91csw0
eefnzdgi11kl
eebetq9fth1o
cen8tzfno5s4


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

萋小磊 发表于 2022-5-15 18:07
没意义的操作, 因为 ws send 是阻塞,  多线程 上下文切换只会更慢
 楼主| weilai911 发表于 2022-5-16 08:33
萋小磊 发表于 2022-5-15 18:07
没意义的操作, 因为 ws send 是阻塞,  多线程 上下文切换只会更慢

我只想到达多线程并发的效果
 楼主| weilai911 发表于 2022-5-19 08:50
 楼主| weilai911 发表于 2022-5-23 14:37
求助求助可CB
80233 发表于 2022-5-23 18:39
写了多线程的,但是没有环境跑不了,楼主可以试试改改
[Python] 纯文本查看 复制代码
import websocket
from concurrent.futures import ThreadPoolExecutor
import time
import sys


def on_message(ws, message):
    print(message)


def on_error(ws, error):
    print(error)


def on_close(ws):
    print("### closed, reconnecting... ###")
    ws.run_forever()


def on_open(ws):
    ws.send('[bct295cneqru]')

    def run(*args):
        time.sleep(20)
        ws.send("2")
    thread_num = 8
    executor = ThreadPoolExecutor(max_workers=thread_num)
    while True:
        executor.submit(run)
    print("重新连接吧")


if __name__ == "__main__":
    websocket.enableTrace(False)
    host = "ws://127.0.0.1:8080"
    ws = websocket.WebSocketApp(host,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    ws.on_open = on_open
    ws.run_forever()
 楼主| weilai911 发表于 2022-5-24 09:32
80233 发表于 2022-5-23 18:39
写了多线程的,但是没有环境跑不了,楼主可以试试改改
[mw_shl_code=python,true]import websocket
from  ...

感谢您的回复 我不太了解多线程 请问一下我下面列出来的值 如何同时专递进去
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 11:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表