Running multithreading in python is suspended
I test multithreading in python, but the code is suspended:
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get())
def save_to_mongo_with_thread():
q = queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(size):
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(size):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
the result is what I wanted, but the program is not ended, the result is:
0 : [0]
1 : [1]
2 : [2]
3 : [3]
4 : [4]
5 : [5]
+++++++++++++++++++++++
0 : [6]
1 : [7]
2 : [8]
3 : [9]
4 : [10]
5 : [11]
+++++++++++++++++++++++
0 : [12]
1 : [13]
2 : [14]
3 : [15]
But it does not print the last +++++++++++++++++++++++
, How can I deal with it?
python multithreading
add a comment |
I test multithreading in python, but the code is suspended:
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get())
def save_to_mongo_with_thread():
q = queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(size):
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(size):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
the result is what I wanted, but the program is not ended, the result is:
0 : [0]
1 : [1]
2 : [2]
3 : [3]
4 : [4]
5 : [5]
+++++++++++++++++++++++
0 : [6]
1 : [7]
2 : [8]
3 : [9]
4 : [10]
5 : [11]
+++++++++++++++++++++++
0 : [12]
1 : [13]
2 : [14]
3 : [15]
But it does not print the last +++++++++++++++++++++++
, How can I deal with it?
python multithreading
add a comment |
I test multithreading in python, but the code is suspended:
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get())
def save_to_mongo_with_thread():
q = queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(size):
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(size):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
the result is what I wanted, but the program is not ended, the result is:
0 : [0]
1 : [1]
2 : [2]
3 : [3]
4 : [4]
5 : [5]
+++++++++++++++++++++++
0 : [6]
1 : [7]
2 : [8]
3 : [9]
4 : [10]
5 : [11]
+++++++++++++++++++++++
0 : [12]
1 : [13]
2 : [14]
3 : [15]
But it does not print the last +++++++++++++++++++++++
, How can I deal with it?
python multithreading
I test multithreading in python, but the code is suspended:
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get())
def save_to_mongo_with_thread():
q = queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(size):
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(size):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
the result is what I wanted, but the program is not ended, the result is:
0 : [0]
1 : [1]
2 : [2]
3 : [3]
4 : [4]
5 : [5]
+++++++++++++++++++++++
0 : [6]
1 : [7]
2 : [8]
3 : [9]
4 : [10]
5 : [11]
+++++++++++++++++++++++
0 : [12]
1 : [13]
2 : [14]
3 : [15]
But it does not print the last +++++++++++++++++++++++
, How can I deal with it?
python multithreading
python multithreading
asked Jan 3 at 7:51
littlelylittlely
299315
299315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You are calling q.put()
16
times - i in range(e * 5, (e + 1)*5)
for every e in range(5)
produces
i in range(0, 5)
i in range(5, 10)
i in range(10, 15)
i in range(15, 16)
i in range(20, 16)
i in range(25, 16)
Which will append 16
values to q
. However you create 25
threads with the associated 25
calls to q.get()
- but once the first 16
elements have been removed get()
blocks. If you use q.get(block=False)
the code will finish successfully but you will get a number of warnings raising Empty
from get()
. Changing the loop condition of the loop
for i in range(size):
threads.append(MongoInsertThread(q, i))
to range(e * size, min((e + 1) * size, 16))
Will fix the mismatch. However it is also useful to add a break statement to stop the outermost for
loop after all 16
elements are added and removed.
Complete Example
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get(block=False))
def save_to_mongo_with_thread():
q = Queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(5):
if (e*size > 16):
break
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(e * size, min((e + 1) * size, 16)):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
Output -
0: [0]
1: [1]
2: [2]
3: [3]
4: [4]
+++++++++++++++++++++++
5: [5]
6: [6]
7: [7]
8: [8]
9: [9]
+++++++++++++++++++++++
10: [10]
11: [11]
12: [12]
13: [13]
14: [14]
+++++++++++++++++++++++
15: [15]
+++++++++++++++++++++++
Also note that I am using get(block = False)
to help illuminate future issues, but it is unnecessary for this example.
@littlely Don't forget to accept if this correctly answers your question
– William Miller
Jan 9 at 2:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018295%2frunning-multithreading-in-python-is-suspended%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are calling q.put()
16
times - i in range(e * 5, (e + 1)*5)
for every e in range(5)
produces
i in range(0, 5)
i in range(5, 10)
i in range(10, 15)
i in range(15, 16)
i in range(20, 16)
i in range(25, 16)
Which will append 16
values to q
. However you create 25
threads with the associated 25
calls to q.get()
- but once the first 16
elements have been removed get()
blocks. If you use q.get(block=False)
the code will finish successfully but you will get a number of warnings raising Empty
from get()
. Changing the loop condition of the loop
for i in range(size):
threads.append(MongoInsertThread(q, i))
to range(e * size, min((e + 1) * size, 16))
Will fix the mismatch. However it is also useful to add a break statement to stop the outermost for
loop after all 16
elements are added and removed.
Complete Example
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get(block=False))
def save_to_mongo_with_thread():
q = Queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(5):
if (e*size > 16):
break
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(e * size, min((e + 1) * size, 16)):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
Output -
0: [0]
1: [1]
2: [2]
3: [3]
4: [4]
+++++++++++++++++++++++
5: [5]
6: [6]
7: [7]
8: [8]
9: [9]
+++++++++++++++++++++++
10: [10]
11: [11]
12: [12]
13: [13]
14: [14]
+++++++++++++++++++++++
15: [15]
+++++++++++++++++++++++
Also note that I am using get(block = False)
to help illuminate future issues, but it is unnecessary for this example.
@littlely Don't forget to accept if this correctly answers your question
– William Miller
Jan 9 at 2:14
add a comment |
You are calling q.put()
16
times - i in range(e * 5, (e + 1)*5)
for every e in range(5)
produces
i in range(0, 5)
i in range(5, 10)
i in range(10, 15)
i in range(15, 16)
i in range(20, 16)
i in range(25, 16)
Which will append 16
values to q
. However you create 25
threads with the associated 25
calls to q.get()
- but once the first 16
elements have been removed get()
blocks. If you use q.get(block=False)
the code will finish successfully but you will get a number of warnings raising Empty
from get()
. Changing the loop condition of the loop
for i in range(size):
threads.append(MongoInsertThread(q, i))
to range(e * size, min((e + 1) * size, 16))
Will fix the mismatch. However it is also useful to add a break statement to stop the outermost for
loop after all 16
elements are added and removed.
Complete Example
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get(block=False))
def save_to_mongo_with_thread():
q = Queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(5):
if (e*size > 16):
break
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(e * size, min((e + 1) * size, 16)):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
Output -
0: [0]
1: [1]
2: [2]
3: [3]
4: [4]
+++++++++++++++++++++++
5: [5]
6: [6]
7: [7]
8: [8]
9: [9]
+++++++++++++++++++++++
10: [10]
11: [11]
12: [12]
13: [13]
14: [14]
+++++++++++++++++++++++
15: [15]
+++++++++++++++++++++++
Also note that I am using get(block = False)
to help illuminate future issues, but it is unnecessary for this example.
@littlely Don't forget to accept if this correctly answers your question
– William Miller
Jan 9 at 2:14
add a comment |
You are calling q.put()
16
times - i in range(e * 5, (e + 1)*5)
for every e in range(5)
produces
i in range(0, 5)
i in range(5, 10)
i in range(10, 15)
i in range(15, 16)
i in range(20, 16)
i in range(25, 16)
Which will append 16
values to q
. However you create 25
threads with the associated 25
calls to q.get()
- but once the first 16
elements have been removed get()
blocks. If you use q.get(block=False)
the code will finish successfully but you will get a number of warnings raising Empty
from get()
. Changing the loop condition of the loop
for i in range(size):
threads.append(MongoInsertThread(q, i))
to range(e * size, min((e + 1) * size, 16))
Will fix the mismatch. However it is also useful to add a break statement to stop the outermost for
loop after all 16
elements are added and removed.
Complete Example
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get(block=False))
def save_to_mongo_with_thread():
q = Queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(5):
if (e*size > 16):
break
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(e * size, min((e + 1) * size, 16)):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
Output -
0: [0]
1: [1]
2: [2]
3: [3]
4: [4]
+++++++++++++++++++++++
5: [5]
6: [6]
7: [7]
8: [8]
9: [9]
+++++++++++++++++++++++
10: [10]
11: [11]
12: [12]
13: [13]
14: [14]
+++++++++++++++++++++++
15: [15]
+++++++++++++++++++++++
Also note that I am using get(block = False)
to help illuminate future issues, but it is unnecessary for this example.
You are calling q.put()
16
times - i in range(e * 5, (e + 1)*5)
for every e in range(5)
produces
i in range(0, 5)
i in range(5, 10)
i in range(10, 15)
i in range(15, 16)
i in range(20, 16)
i in range(25, 16)
Which will append 16
values to q
. However you create 25
threads with the associated 25
calls to q.get()
- but once the first 16
elements have been removed get()
blocks. If you use q.get(block=False)
the code will finish successfully but you will get a number of warnings raising Empty
from get()
. Changing the loop condition of the loop
for i in range(size):
threads.append(MongoInsertThread(q, i))
to range(e * size, min((e + 1) * size, 16))
Will fix the mismatch. However it is also useful to add a break statement to stop the outermost for
loop after all 16
elements are added and removed.
Complete Example
class MongoInsertThread(threading.Thread):
def __init__(self, queue, thread_id):
super(MongoInsertThread, self).__init__()
self.thread_id = thread_id
self.queue = queue
def run(self):
print(self.thread_id,': ', self.queue.get(block=False))
def save_to_mongo_with_thread():
q = Queue.Queue()
size = int(math.ceil(16 / 3))
for e in range(5):
if (e*size > 16):
break
for i in range(e * size, min((e + 1) * size, 16)):
q.put([i], block=False)
threads =
for i in range(e * size, min((e + 1) * size, 16)):
threads.append(MongoInsertThread(q, i))
for t in threads:
t.start()
for t in threads:
t.join()
print("+++++++++++++++++++++++")
Output -
0: [0]
1: [1]
2: [2]
3: [3]
4: [4]
+++++++++++++++++++++++
5: [5]
6: [6]
7: [7]
8: [8]
9: [9]
+++++++++++++++++++++++
10: [10]
11: [11]
12: [12]
13: [13]
14: [14]
+++++++++++++++++++++++
15: [15]
+++++++++++++++++++++++
Also note that I am using get(block = False)
to help illuminate future issues, but it is unnecessary for this example.
answered Jan 4 at 3:17
William MillerWilliam Miller
1,570317
1,570317
@littlely Don't forget to accept if this correctly answers your question
– William Miller
Jan 9 at 2:14
add a comment |
@littlely Don't forget to accept if this correctly answers your question
– William Miller
Jan 9 at 2:14
@littlely Don't forget to accept if this correctly answers your question
– William Miller
Jan 9 at 2:14
@littlely Don't forget to accept if this correctly answers your question
– William Miller
Jan 9 at 2:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018295%2frunning-multithreading-in-python-is-suspended%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown