Python threading in for loop
Date : March 29 2020, 07:55 AM
wish helps you Here is a simple Sublime Text 2 plugin with threading. What it does is insert Hello World! after 3 seconds. What you'll notice is that you can still move the cursor during those three seconds. In your case, it looks like you just need to grab a bunch of snippets from an API and create a context menu from the returned data. Then there will be a notification at the bottom telling you how many snippets were added. I could be wrong, but you should be able to modify this code to make your plugin work. import threading
import time
import sublime
import sublime_plugin
"""
The command just creates and runs a thread.
The thread will do all the work in the background.
Note that in your Thread constructor, you will need to pass in an
instance of your Command class to work with in your thread.
"""
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
exampleThread = ExampleThread(self, edit)
exampleThread.start()
"""
Extend the Thread class and add your functionality in
the run method below.
One thing to remember when moving your code over is
you need to use self.cmd instead of self.
"""
class ExampleThread(threading.Thread):
"""
Remember to pass in the parameters you need
in this thread constructor.
"""
def __init__(self, cmd, edit):
threading.Thread.__init__(self)
self.cmd = cmd
self.edit = edit
"""
Add your functionality here.
If you need to access the main thread, you need to
use sublime.set_timeout(self.callback, 1).
In my example here, you can't call insert text into the editor
unless you are in the main thread.
Luckily that is fast operation.
Basically, time.sleep(3) is a slow operation and will block, hence it
is run in this separate thread.
"""
def run(self):
time.sleep(3)
sublime.set_timeout(self.callback, 1)
"""
This is the callback function that will be called to
insert HelloWorld.
You will probably need to use this to set your status message at
the end. I'm pretty sure that requires that you be on main thread
to work.
"""
def callback(self):
self.cmd.view.insert(self.edit, 0, "Hello, World!")
import threading
import time
import sublime
import sublime_plugin
def buildsnippetURL():
return ""
def getsnippets(snippet_url):
time.sleep(3)
return ""
class SyncsnippetsCommand(sublime_plugin.TextCommand):
def run(self, edit):
syncsnippetsThread = SyncsnippetsThread(self, edit)
syncsnippetsThread.start()
class SyncsnippetsThread(threading.Thread):
def __init__(self, cmd, edit):
threading.Thread.__init__(self)
self.cmd = cmd
self.edit = edit
def buildLexerDict(self,snippets):
lexers = snippets[0]['user']['lexers']
lexer_dict = {}
for lexer in lexers:
lexer_dict[lexer] = []
return lexer_dict
def buildsnippetsContextDict(self,snippets,lexer_dict):
snippets_dict = lexer_dict
for snippet in snippets:
snippets_dict[snippet['lexer']].append({"id":str(snippet['id']),
"title":snippet['title']})
return snippets_dict
def run(self):
snippet_url = buildsnippetURL()
snippets_count = 1;
snippets = getsnippets(snippet_url)
"""
context_menu = '['
context_menu += '\n\t{ "caption": "snippets", "id": "file", "children":'
context_menu += '\n\t\t['
if snippets == None:
{"caption":"No snippets available"}
else:
snippets = snippets['objects']
lexers = self.buildLexerDict(snippets)
snippets_dict = self.buildsnippetsContextDict(snippets, lexers)
for j,key in reversed(list(enumerate(reversed(snippets_dict.keys())))):
... loop through JSON and create menu ...
if j == 0:
context_menu += ''
else:
context_menu += ','
context_menu += '\n\t\t]'
context_menu += '\n\t}'
context_menu += '\n]'
f = open(sublime.packages_path() + '\snippetSync\\Context.sublime-menu', 'w')
f.write(context_menu)
f.close
"""
sublime.set_timeout(lambda: self.callback(snippets_count), 1)
def callback(self, snippets_count):
self.cmd.view.set_status('snippet', 'snippet Sync: Added ' + str(snippets_count) + ' snippets from your account.')
sublime.set_timeout(lambda: self.cmd.view.erase_status('snippet'), 3000)
|
python - multi-threading in a for loop
Date : March 29 2020, 07:55 AM
With these it helps Here is a slightly modified version using a semaphore objectimport threading
import Queue
NUM_THREADS = 2 # you can change this if you want
semaphore = threading.Semaphore(NUM_THREADS)
threads = NUM_THREADS
running_threads = []
lock = threading.Lock()
q = Queue.Queue()
# moved the check function out of the loop
def check(name, q, s):
# acquire the semaphore
with s:
not_empty = True
while not_empty:
try:
email = q.get(False) # we are passing false so it won't block.
except Queue.Empty, e:
not_empty = False
break
lock.acquire()
print email, name, threading.active_count()
lock.release()
# additional work ...
q.task_done()
for x in open(names):
name = x.strip()
for word in open(emails):
q.put(word.strip())
for i in range(threads):
t = threading.Thread(target=check, args=(name, q, semaphore))
# t.setDaemon(True) # we are not setting the damenon flag
t.start()
running_threads.append(t)
# joining threads (we need this if the daemon flag is false)
for t in running_threads:
t.join()
# joining queue (Probably won't need this if the daemon flag is false)
q.join()
|
Threading / While Loop with python
Tag : python , By : Jesenko Mehmedbasic
Date : March 29 2020, 07:55 AM
To fix the issue you can do How do you know it runs only once? Have you debugged it or do you expect to have the correct result when code reaches this part? with open('datafile.csv', 'r') as fp:
....
#Parse the xml
_t = threading.Timer(5.0, parseXML)
_t.daemon = True
_t.start()
with urllib.request.urlopen(....)
foo = threading.Lock()
....
....
with foo:
with open(...) as fp:
....
|
Python threading outperforms simple while loop OR threading Optimization
Date : March 29 2020, 07:55 AM
|
Python threading in a loop but with max threads
Date : March 29 2020, 07:55 AM
|