Use quit channel.
This commit is contained in:
		
							parent
							
								
									5a19118dae
								
							
						
					
					
						commit
						c371fd6aab
					
				
					 1 changed files with 7 additions and 5 deletions
				
			
		
							
								
								
									
										12
									
								
								main.go
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								main.go
									
										
									
									
									
								
							|  | @ -9,7 +9,6 @@ import ( | |||
| 	"os" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
|  | @ -29,8 +28,9 @@ func main() { | |||
| 	defer file.Close() | ||||
| 
 | ||||
| 	c := make(chan string, *worker) | ||||
| 	quit := make(chan bool) | ||||
| 	for i := 0; i < *worker; i++ { | ||||
| 		go processFileEntry(c) | ||||
| 		go processFileEntry(c, quit) | ||||
| 	} | ||||
| 
 | ||||
| 	scanner := bufio.NewScanner(file) | ||||
|  | @ -39,14 +39,16 @@ func main() { | |||
| 	} | ||||
| 	close(c) | ||||
| 
 | ||||
| 	// Let's wait 10 sec for the last images to download. | ||||
| 	time.Sleep(10 * 1e9) | ||||
| 	for i := 0; i < *worker; i++ { | ||||
| 		<-quit | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func processFileEntry(c <-chan string) { | ||||
| func processFileEntry(c <-chan string, quit chan<- bool) { | ||||
| 	for str := range c { | ||||
| 		downloadFile(str) | ||||
| 	} | ||||
| 	quit <- true | ||||
| } | ||||
| 
 | ||||
| func downloadFile(url string) { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue