标签: python-multithreading

如果 __init__ 只调用 super.__init__ 是否有必要?

我正在对类进行子threading.Thread类化,它目前看起来像这样:

class MyThread(threading.Thread):
    def __init__(self:
        super(MyThread, self).__init__()

    def run(self):
        # Do some stuff
Run Code Online (Sandbox Code Playgroud)

__init__在这种情况下是必需的吗?如果我省略它,它会自动调用吗?

python oop inheritance python-multithreading python-2.7

5
推荐指数
1
解决办法
327
查看次数

使用 Pyramid 和 rq 写入相同的日志文件

我正在使用 Pyramid 和rq,我想将 rq 工作作业记录在与我的 Pyramid 应用程序相同的文件中。

我的设置如下所示:

开发文件

# regular pyramid setup here...

[loggers]
keys = root, main

[handlers]
keys = console, debuglog

[formatters]
keys = generic

[logger_root]
level = DEBUG
handlers = console, debuglog

[logger_main]
level = DEBUG
handlers =
qualname = main

[handler_debuglog]
class = handlers.RotatingFileHandler
args = ('%(here)s/__logs/pyramid_debug.log','a')
level = DEBUG
formatter = generic

[formatter_generic]
format = %(asctime)s %(levelname)-8s [%(name)s][%(threadName)s] %(message)s
Run Code Online (Sandbox Code Playgroud)

主文件

from bunch import Bunch
from redis import Redis
from rq import Queue, …
Run Code Online (Sandbox Code Playgroud)

python logging multithreading python-multithreading pyramid

5
推荐指数
1
解决办法
1511
查看次数

如何通过线程在python中使用Event对象?

我有一个主要的 python 脚本,它启动一个在后台运行的线程。

poll = threading.Thread(target=poll_files, args=myargs)

我希望我的主脚本等到我的poll线程中发生特定的事情。我想使用一个 Event 对象。所以在我的主脚本中,我这样做:

trigger = threading.Event()

当我想等待时:

trigger.wait()

我的问题是,在我的poll线程中,如何将事件设置为 True?我知道我这样做:

trigger.set()

但我还需要trigger = threading.Event()在我的poll线程里面吗?

python events python-multithreading

5
推荐指数
1
解决办法
5985
查看次数

Python Webdriver 多线程

我正在尝试使用以下代码生成多个 webdriver 实例:http : //www.ibm.com/developerworks/aix/library/au-threadingpython/

import time
import Queue
import urllib2
import threading
from selenium import webdriver
from BeautifulSoup import BeautifulSoup
hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
    "http://ibm.com", "http://apple.com"]
queue = Queue.Queue
out_queue = Queue.Queue

class Login_Driver(threading.Thread):
    def __init__(self, queue, out_queue, driver):
        threading.Thread.__init__(self)
        self.queue = queue
        self.out_queue = out_queue
        self.driver = driver
        print driver.title
    def run(self):
        while True:
            #grabs host from queue
            host = self.queue.get()
            #grabs urls of hosts and then grabs chunk of webpage
            driver.get(host)
            chunk = driver.page_source()
            #place …
Run Code Online (Sandbox Code Playgroud)

python python-multithreading selenium-webdriver

5
推荐指数
1
解决办法
4424
查看次数

Python 子进程 Popen.terminate() 仍然停留在 wait()

我正在尝试使用 RPi 实现无线电播放器。目标是设置播放列表并在播放列表填充后开始播放。一旦停止代码被执行,播放器和收音机进程就会退出。

无线电进程很好地终止,但播放器进程即使在调用终止后仍然保持等待。如果再次调用停止代码,则播放器进程终止

尝试的事情:

  1. 重新排序等待命令(播放器,收音机)/(收音机,播放器)
  2. 类似地重新排序终止命令
  3. 使用 kill 而不是终止挂起 RPi

玩家代码:

while playlist:
    player = subprocess.Popen(
            ["avconv", "-i", "Music/%s" % playlist[0], "-f", "s16le", "-ar", 
            "22.05k", "-ac", "1", "-"], stdout=subprocess.PIPE)

    radio = subprocess.Popen(["./pifm", "-", freq], stdin=player.stdout)

    radio.wait()
    print "************ exiting from radio :)"
    player.wait()
    print "************ exiting from player :)"

    playlist.pop(0)
player = None
radio = None
Run Code Online (Sandbox Code Playgroud)

播放器停止代码(从另一个线程调用):

print "************ stop requested"

if radio and radio.poll() is None:
    print "************ terminating radio :)"
    radio.terminate()

if player and player.poll() is None: …
Run Code Online (Sandbox Code Playgroud)

python subprocess python-multithreading raspberry-pi

5
推荐指数
1
解决办法
1320
查看次数

多线程 HTTP GET 请求在大约 900 次下载后严重减慢

我正在尝试使用从 Amazon S3 下载大约 3,000 个文件(每个文件的大小可能为 3 MB)requests_futures,但下载速度在大约 900 个后严重减慢,并且实际上开始运行速度比基本的 for 循环慢。

我似乎没有耗尽内存或 CPU 带宽。然而,看起来我机器上的 Wifi 连接速度几乎没有变慢:我从几千个数据包/秒下降到只有 3-4 个。最奇怪的是,在 Python 进程退出并重新启动 wifi 适配器之前,我无法加载任何网站。

到底是什么原因导致了这种情况,我该如何调试它?

如果有帮助,这是我的 Python 代码:

import requests
from requests_futures.sessions import FuturesSession
from concurrent.futures import ThreadPoolExecutor, as_completed

# get a nice progress bar
from tqdm import tqdm

def download_threaded(urls, thread_pool, session):
    futures_session = FuturesSession(executor=thread_pool, session=session)
    futures_mapping = {}
    for i, url in enumerate(urls):
        future = futures_session.get(url)
        futures_mapping[future] = i
    
    results = [None] * len(futures_mapping)

    with …
Run Code Online (Sandbox Code Playgroud)

multithreading python-multithreading python-3.x python-requests concurrent.futures

5
推荐指数
1
解决办法
624
查看次数

Python与多线程共享数据库连接

我正在创建一个数据库连接,并在多个线程(大约 100 到 1000 个线程)之间共享它,我收到如下错误,

1)我的代码有什么问题?

这是我的示例代码,我将针对以下场景更改此代码: 基于一些 SQL 结果,它将创建最多 1000 个线程。 如果我为每个线程创建数据库连接,它将创建更多的数据库连接。为了避免这种情况,我在所有线程中使用了相同的数据库连接,我只有插入和更新操作。

2)这种多线程场景有没有更好的方法?

错误:

Worker: 0
Create Notification called for userid abcxyz and message is Test Message : Worker: 0 Attempt: 0
Sleeping ... 

Worker: 1
Create Notification called for userid abcxyz and message is Test Message : Worker: 1 Attempt: 0

Partial insertedID 43
Traceback (most recent call last):
  File "T:\work\Testing\src\notification_data_feeder.py", line 58, in createNotification
    db.commit()
OperationalError: (2006, 'MySQL server has gone away')

Error on createNotification 
Traceback (most …
Run Code Online (Sandbox Code Playgroud)

python multithreading mysql-python threadpool python-multithreading

5
推荐指数
0
解决办法
1691
查看次数

如何使用 py.test 测试并发性

我想使用 py.test 测试函数的线程安全性我的努力:

def function_to_be_tested(arg1,arg2):
   some functionality

class Test:
   def setup()
   def teardown()
   def test_conc()
       p1=Process(taget=function_to_be_tested,args(arg1,arg2,))
       p2=Process (taget=function_to_be_tested,args(arg1,arg3,))
       p1.start()
       p2.start()
       p1.join()
       p2.join()
Run Code Online (Sandbox Code Playgroud)

使用 py.test 命令执行上面的文件。这显示以下错误。

ExceptionPexpect: isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?
Run Code Online (Sandbox Code Playgroud)

你能帮我解码这个错误,并就如何做到这一点提供指导吗?

谢谢这是我正在尝试的实际代码和堆栈跟踪:

import pytest
from multiprocessing import Process
from pexpect import pxssh

def func(cls,b):
    cls.s.sendline("bteq")
    cls.s.prompt()
    print b
    #some operations inside the bteq session

class Test:
    @classmethod
    def setup_class(cls):
        cls.s=pxssh.pxssh()
        cls.s.login("IP",'Username','Pwd')
    @classmethod
    def …
Run Code Online (Sandbox Code Playgroud)

python concurrency pytest python-multithreading python-multiprocessing

5
推荐指数
1
解决办法
1172
查看次数

如何等待 RxPy 并行线程完成

基于这个优秀的 SO 答案,我可以在 RxPy 中并行处理多个任务,我的问题是你如何等待它们全部完成?我知道我可以使用线程,.join()但是 Rx 调度器似乎没有任何这样的选项。.to_blocking()也无济于事, MainThread 在触发所有通知并调用完整处理程序之前完成。下面是一个例子:

from __future__ import print_function
import os, sys
import time
import random
from rx import Observable
from rx.core import Scheduler
from threading import current_thread

def printthread(val):
    print("{}, thread: {}".format(val, current_thread().name))

def intense_calculation(value):
    printthread("calc {}".format(value))
    time.sleep(random.randint(5, 20) * .1)
    return value

if __name__ == "__main__":
    Observable.range(1, 3) \
        .select_many(lambda i: Observable.start(lambda: intense_calculation(i), scheduler=Scheduler.timeout)) \
        .observe_on(Scheduler.event_loop) \
        .subscribe(
            on_next=lambda x: printthread("on_next: {}".format(x)),
            on_completed=lambda: printthread("on_completed"),
            on_error=lambda err: printthread("on_error: {}".format(err))) …
Run Code Online (Sandbox Code Playgroud)

python multithreading python-multithreading rx-py reactivex

5
推荐指数
2
解决办法
2627
查看次数

从错误的线程中垃圾收集的 Tkinter 对象

我似乎tkinter通过使用一些多线程来破坏linux。据我所知,我设法在不是主 GUI 线程的线程上触发垃圾回收。这导致__del__在一个tk.StringVar实例上运行,该实例试图tcl从错误的线程调用堆栈,从而导致 linux 上的混乱。

下面的代码是我能想出的最小例子。请注意,我没有用 做任何实际工作matplotlib,但否则我无法触发问题。在__del__对方法Widget验证的Widget情况下被从其他线程删除。典型的输出是:

Running off thread on 140653207140096
Being deleted... <__main__.Widget object .!widget2> 140653210118576
Thread is 140653207140096
... (omitted stack from from `matplotlib`
  File "/nfs/see-fs-02_users/matmdpd/anaconda3/lib/python3.6/site-packages/matplotlib/text.py", line 218, in __init__
    elif is_string_like(fontproperties):
  File "/nfs/see-fs-02_users/matmdpd/anaconda3/lib/python3.6/site-packages/matplotlib/cbook.py", line 693, in is_string_like
    obj + ''
  File "tk_threading.py", line 27, in __del__
    traceback.print_stack()
...
Exception ignored in: <bound method Variable.__del__ of <tkinter.StringVar object at 0x7fec60a02ac8>>
Traceback …
Run Code Online (Sandbox Code Playgroud)

tkinter matplotlib python-multithreading python-3.x

5
推荐指数
1
解决办法
1363
查看次数