Comments (6)
Please fix the format of your markdown:
63:62 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]
generated by check-issue
from rod.
Have you tried this?
Lines 533 to 574 in 46baf3a
Also the browser pool works fine to me:
Lines 599 to 637 in 167ecc0
from rod.
Yea tried both. They both work because the pool/browser is created in the same function lifetime. I’m trying to have a singleton that multiple top level functions refer to over time
from rod.
have a singleton that multiple top level functions refer to over time
Sorry, I'm not sure I understand what you mean. If you just need a global variable, then it's just a code design issue, not related to rod, such as use a closure:
var globalPool = func() func() (*rod.Page, func()) {
pool := rod.NewPagePool(3)
create := func() *rod.Page {
browser := rod.New().MustConnect()
return browser.MustIncognito().MustPage()
}
return func() (*rod.Page, func()) {
page := pool.Get(create)
return page, func() {
pool.Put(page)
}
}
}()
func main() {
for range "...." {
go func() {
page, release := globalPool()
defer release()
page.MustNavigate("http://example.com")
}()
}
}
from rod.
I think my problem was that I was using defer l.Cleanup()
, which I now see waits until the browser(s) it launched are closed.
Thank you for your snippet. I now have this code running & working; however, I'd still like to make it so that a browser is reused and the pool is creating Page
from the same Browser struct (instead of creating new browsers every time, like in your code). The reason for this is that I have expensive logic running to figure out which browser to use & that only needs to be run once
package main
import (
"fmt"
"sync"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
)
var globalPool = func() func() (*rod.Page, func()) {
pool := rod.NewPagePool(3)
fmt.Println("attempting...")
create := func() *rod.Page {
l := launcher.New().Headless(true)
if path, exists := launcher.LookPath(); exists {
fmt.Println("~> found local browser")
l.Bin(path)
}
// defer l.Cleanup()
browser := rod.New().ControlURL(l.MustLaunch()).MustConnect()
return browser.MustIncognito().MustPage()
}
return func() (*rod.Page, func()) {
fmt.Println("before create")
page := pool.Get(create)
fmt.Println("found page")
return page, func() {
fmt.Println("> released")
pool.Put(page)
}
}
}()
func main() {
var wg sync.WaitGroup
for v := range []int{1, 2, 3} {
fmt.Println("inside loop")
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("inside goroutine")
page, release := globalPool()
defer release()
fmt.Println(">> eval", string(v))
page.MustEval("function(x) { console.log('~>', x) }", v)
}()
}
wg.Wait()
}
from rod.
I got it working now, thank you :) It just came down to using l.Cleanup()
, which I should be calling manually elsewhere.
from rod.
Related Issues (20)
- 当浏览器关闭时,chrome_crashpad_handler和Chromium Helper两个进程没有被关闭。 HOT 3
- Is it possible to improve Screenshot's fullPage implementation? HOT 1
- Why doesn't it remember the window size now? HOT 11
- Bug Select Frame Element HOT 2
- Bug element's pointer-events is none HOT 4
- Download in headless mode is failing, but works fine otherwise HOT 6
- Dear go-rod production team, I would like to ask you a question about remote connections and using proxies. HOT 3
- Is there a way to get css rules associated with an element ? HOT 2
- Add a JS script to browser HOT 3
- Missing Request Body When Loading "https://www.booking.com" Using go-rod HOT 2
- [bug] Page.EachEvent call is not reusable for the second time HOT 2
- but cannot check the element nodes on go-rod. HOT 5
- I have a print button on UI, on clicking that the print dialog box doesn't open in browser launched from rod. HOT 6
- Is there any way to wait for a certain element in the page: HOT 5
- Any way to launch multiple browsers with different users each? HOT 1
- Exporting the full page as PDF (single page) HOT 1
- Ignoring the error "element's pointer-events is none" HOT 2
- 进程启动被杀后 无法退出 导致锁死 HOT 3
- Upload file on Managed Launcher? HOT 2
- Does rod support separate sessions or incognito mode? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rod.