Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python线程错误,Twitch脚本_Python_Multithreading_Sockets - Fatal编程技术网

Python线程错误,Twitch脚本

Python线程错误,Twitch脚本,python,multithreading,sockets,Python,Multithreading,Sockets,我正在运行这个脚本,但我似乎无法让它工作任何想法?我像python filename.py 10一样运行它,但是它返回了线程错误,我假设它是在代码中打印的。我怎样才能解决这个问题 import requests import subprocess import json import sys import threading import time from Queue import Queue numberOfViewers = int(sys.argv[1]) builderThreads

我正在运行这个脚本,但我似乎无法让它工作任何想法?我像python filename.py 10一样运行它,但是它返回了线程错误,我假设它是在代码中打印的。我怎样才能解决这个问题

import requests
import subprocess
import json
import sys
import threading
import time
from Queue import Queue

numberOfViewers = int(sys.argv[1])
builderThreads = int(sys.argv[2])
startTime = time.time()
numberOfSockets = 0
concurrent = 25
urls = []
urlsUsed = []

def getURL(): # Get tokens
  output = subprocess.Popen(["livestreamer", "twitch.tv/The_XPZ", "-j"],     stdout=subprocess.PIPE).communicate()[0]
  return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter

def build(): # Builds a set of tokens, aka viewers
    global numberOfSockets
    global numberOfViewers
    while True:
        if numberOfSockets < numberOfViewers:
            numberOfSockets += 1
            print "Building viewers " + str(numberOfSockets) + "/" + str(numberOfViewers)
            urls.append(getURL())

def view(): # Opens connections to send views
    global numberOfSockets
    while True:
        url=q.get()
        requests.head(url)
        if (url in urlsUsed):
            urls.remove(url)
            urlsUsed.remove(url)
            numberOfSockets -= 1
        else:
            urlsUsed.append(url)
        q.task_done()

if __name__ == '__main__':
    for i in range(0, builderThreads):
        threading.Thread(target = build).start()

    while True:
        while (numberOfViewers != numberOfSockets): # Wait until sockets are built
            time.sleep(1)

        q=Queue(concurrent*2)
        for i in range(concurrent):
            try:
                t=threading.Thread(target=view)
                t.daemon=True
                t.start()
            except:
                print 'thread error'
        try:
            for url in urls:
                print url
                q.put(url.strip())
                q.join()
        except KeyboardInterrupt:
            sys.exit(1)

至少现在,线程实现没有遇到错误。问题是try/except块捕获每种类型的错误,并打印线程错误,而不管它是什么

不是为打开URL而设计的。Windows正在引发错误,因为您指定的URL/网络信息不是本地文件或有效命令

相反,您需要查看一个库,比如or,它旨在通过HTTP请求获取信息。假设您需要json数据,那么可以使用json库对其进行解码。例如:

import urllib2

def getURL():
    output = urllib2.urlopen('http://www.twitch.tv/The_XPZ') # Any URL
    return json.load(response)['streams']['worst']['url']

您的代码不清楚您试图访问的URL是什么,但您可以替换您需要的。

您能给我们完整的回溯吗?原始代码在这里,是您尝试运行脚本时收到的完整错误消息。回溯最近一次调用:文件C:\Python27\viewerbotpy,第9行,在numberOfViewers=intsys.argv[1]indexer中,indexer:list index out-of-rangeWell,这绝对不是线程错误。