我是一个多处理新手,
我对线程有所了解,但我需要提高计算速度,希望通过多处理:
示例描述:将字符串发送到线程,更改字符串+基准测试,将结果发送回打印.
Run Code Online (Sandbox Code Playgroud)from threading import Thread class Alter(Thread): def __init__(self, word): Thread.__init__(self) self.word = word self.word2 = '' def run(self): # Alter string + test processing speed for i in range(80000): self.word2 = self.word2 + self.word # Send a string to be altered thread1 = Alter('foo') thread2 = Alter('bar') thread1.start() thread2.start() #wait for both to finish while thread1.is_alive() == True: pass while thread2.is_alive() == True: pass print(thread1.word2) print(thread2.word2)
目前这需要大约6秒钟,我需要它更快.
我一直在研究多处理,找不到与上面代码相同的东西.我认为我所追求的是汇集,但我发现的例子很难理解.我想利用所有核心(8核),multiprocessing.cpu_count()但我真的只是有关多处理的有用信息,而不足以复制上述代码.如果有人能指出我正确的方向或更好的方向,请提供一个非常感谢的例子.Python 3请
我试图理解理解是如何运作的.
我想循环遍历两个列表,并比较每个列表以找出差异.如果一个/多个单词不同,我想打印这个单词.
我喜欢这一切的代码,这就是为什么我对理解感兴趣.
我是多处理新手
我已经为两个"强烈推荐"的多处理示例运行了示例代码,以响应其他stackoverflow多处理问题.这是一个例子(我不敢再跑了!)
test2.py(从pydev运行)
import multiprocessing
class MyFancyClass(object):
def __init__(self, name):
self.name = name
def do_something(self):
proc_name = multiprocessing.current_process().name
print(proc_name, self.name)
def worker(q):
obj = q.get()
obj.do_something()
queue = multiprocessing.Queue()
p = multiprocessing.Process(target=worker, args=(queue,))
p.start()
queue.put(MyFancyClass('Fancy Dan'))
# Wait for the worker to finish
queue.close()
queue.join_thread()
p.join()
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我的计算机很快就会慢下来.它逐渐变慢.过了一段时间,我设法进入任务管理器只看到进程选项卡下的许多许多python.exe.在尝试结束某些过程后,我的鼠标停止移动.这是我第二次被迫重启.
我太害怕尝试第三个例子......
运行 - 英特尔(R)酷睿(TM)i7 CPU 870 @ 2.93GHz(8个CPU),win7 64上约2.9GHz
如果有人知道这个问题是什么,并且可以提供一个非常简单的多处理示例(发送字符串也是一个多进程) ,改变它并将其送回打印)我将非常感激.
from difflib import *
import urllib.request,urllib.parse,urllib.error
from urllib.parse import unquote
import time
import pdb
try:
file2 = urllib.request.Request('site goes here')
file2.add_header("User-Agent", 'Opera/9.61 (Windows NT 5.1; U; en) Presto/2.1.1')
ResponseData = urllib.request.urlopen(file2).read().decode("utf8", 'ignore')
except urllib.error.URLError as e: print('http'); ResponseData = ''
except socket.error as e: ResponseData = ''
except socket.timeout as e: ResponseData = ''
except UnicodeEncodeError as e: ResponseData = ''
except http.client.BadStatusLine as e: ResponseData = ''
except http.client.IncompleteRead as e: ResponseData = ''
except urllib.error.HTTPError as e: ResponseData …Run Code Online (Sandbox Code Playgroud) 当我尝试单击弹出菜单后面的按钮时,我收到以下错误消息。
*** selenium.common.exceptions.ElementClickInterceptedException: Message: Element <input id="submitButton" class="search-button icon-search active" type="submit"> is not clickable at point (729.2000122070312,22) because another element <div id="monetate_lightbox_mask" class=""> obscures it
Run Code Online (Sandbox Code Playgroud)
此错误消息能够识别阻止我点击的内容的名称
我怎样才能获得这个名称(作为一个元素),以便我可以进行修改,例如,
element = <div id="monetate_lightbox_mask" class="">
browser.execute_script("""var element = arguments[0]; element.parentNode.removeChild(element);""", element)
Run Code Online (Sandbox Code Playgroud)
等待功能不适用,因为此弹出窗口不会消失。我尝试过 webdriver.ActionChains,但它不能解决这个问题
下面的代码有5个worker ....每个都打开自己的worker_task()
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_url = {executor.submit(worker_task, command_, site_): site_ for site_ in URLS}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try: data = future.result()
Run Code Online (Sandbox Code Playgroud)
但是......在每个worker_task()内部......我无法识别......当前正在使用5个工作人员中的哪一个( Worker_ID)
如果我想print('worker 3 has finished')进入worker_task() ......我不能这样做,因为executor.submit不允许
有什么解决办法吗?
python multithreading python-3.x threadpoolexecutor concurrent.futures
我正在寻找一种简单的方法来识别另一个字符串中字符串的最后位置...例如.如果我有:file = C:\Users\User\Desktop\go.py
我想要这样做file = go.py
通常情况下,我必须C:\Users\User\Desktop\go.py通过循环+查找语句,并且Evey时间遇到\它会问...它是\字符串中的最后一个吗?...一旦我找到最后一个\,那么file = file[last\:len(file)]
我很想知道是否有更快的更简洁的方法来做这个...最好没有循环.
像file = [file('\',last之):len(file)]
类的东西如果没有像我上面所示的那样......那么我们可以将循环置于[:]某种方式之内.喜欢的东西file = [for i in ...:len(file)]
谢谢:)
我正在寻找最简单的编码方法.
说我有一个字符串包含: 'the f<ox jumpe>d over the l<azy> dog <and the >fence'
使用<作为开始标记和>作为结束标记,我想将中间的所有内容保存到列表中.
如果保存到list1中,list1将等于['ox jumpe','azy','和']
谁知道一个漂亮,简洁的SHORT方式来做到这一点.
谢谢!
我的脚本创建文件以在运行时间内存储数据,我想在程序终止时删除这些文件.这可能吗?
我不想要一个解决方案的建议......比如'不要创建文件'.
非常感激!
我用 python 编写代码,对 html、mysql、javascript 或其他数据库类型的语言知之甚少。
我正在使用 pythons urllib 模块来检索 Web 源代码,我想知道是否有办法识别网页是否具有动态内容。动态内容我的意思是,任何自主更改的源代码都不是来自用户输入。例如,如果该网页上的广告每 10 分钟更改一次。即使我加载页面两次并比较源代码,它也不会发现页面实际上是动态的。我很想知道是否有任何我可以在源代码中寻找的“关键字”,以识别网页正在使用动态内容。
谢谢
更新:
我并不声称对 javascript 一无所知,但我在我知道是动态的页面中发现了以下代码,但通常不会显示它:
Run Code Online (Sandbox Code Playgroud)document.write('<script language="JavaScript" src="http://ad.doubleclick.net...可能
document.write是识别动态页面的好关键字
python ×9
python-3.x ×3
string ×2
delete-file ×1
dynamic ×1
find ×1
html ×1
http-error ×1
javascript ×1
list ×1
multiprocess ×1
mysql ×1
pool ×1
selenium ×1
tags ×1
termination ×1
urllib ×1